首页 > 编程语言 > 详细

3.1.3 允许多个线程同时访问:信号量

时间:2018-02-07 11:08:42      阅读:217      评论:0      收藏:0      [点我收藏+]
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 允许多个线程同时访问:信号量

原文:https://www.cnblogs.com/anxbb/p/8425529.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!