package 第三章.信号量;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/**
* Created by zzq on 2018/1/23.
*/
public class SimpoTest implements Runnable {
final Semaphore semaphore=new Semaphore(5);
public void run() {
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + "-------获取执行权");
Thread.sleep(2000);
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(20);
final SimpoTest simpoTest=new SimpoTest();
for (int i = 0; i < 10; i++) {
executorService.submit(simpoTest);
}
executorService.shutdown();
}
}
3.1.3 允许多个线程同时访问:信号量
Posted anxbb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3.1.3 允许多个线程同时访问:信号量相关的知识,希望对你有一定的参考价值。
以上是关于3.1.3 允许多个线程同时访问:信号量的主要内容,如果未能解决你的问题,请参考以下文章