一 semaphore多线程访问
1.1 代码
public class Xinhaoliang {
public static void main(String[] args) {
Semaphore semaphore=new Semaphore(3);
for(int k=1;k<8;k++){
final int m=k;
new Thread(new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();
System.out.println("我是"+m+"号车,开进来了........");
Thread.sleep(5000);
System.out.println("我是"+m+"号车,走喽........");
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
}
1.2 执行结果