Semaphore信号量的使用
作者:互联网
package ThreadTest;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class ThreadTest04 {
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(3);//表示3个车位
for (int i = 0; i < 6; i++) {
new Thread(()->{
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + "获得车位");
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName() + "离开车位");
}catch (Exception e){
e.printStackTrace();
}finally {
semaphore.release();
}
}, String.valueOf(i)).start();
}
}
}
标签:currentThread,Thread,使用,System,信号量,semaphore,Semaphore,车位 来源: https://www.cnblogs.com/yoshinb/p/15370629.html