Semaphore
来源:互联网 发布:java 汉字转拼音 编辑:程序博客网 时间:2024/06/06 01:17
一个计数信号量。从概念上讲,信号量维护了一个许可集。如有必要,在许可可用前会阻塞每一个 acquire()
,然后再获取该许可。每个 release()
添加一个许可,从而可能释放一个正在阻塞的获取者。
public class TestSemaphore {public TestSemaphore() {// TODO Auto-generated constructor stub}public static void main(String[] args) {// 线程池ExecutorService exec = Executors.newCachedThreadPool();// 只能5个线程同时访问final Semaphore semp = new Semaphore(5);// 模拟20个客户端访问for (int index = 0; index < 20; index++) {final int NO = index;Runnable run = new Runnable() {public void run() {try {// 获取许可semp.acquire();System.out.println("Accessing: " + NO);Thread.sleep((long) (Math.random() * 10000));// 访问完后,释放semp.release();System.out.println("-----------------"+ semp.availablePermits());} catch (InterruptedException e) {e.printStackTrace();}}};exec.execute(run);}// 退出线程池exec.shutdown();}}
0 0
- Semaphore
- semaphore
- semaphore
- Semaphore
- Semaphore
- semaphore
- Semaphore
- semaphore
- Semaphore
- Semaphore
- Semaphore
- semaphore
- Semaphore
- Semaphore
- semaphore
- Semaphore
- semaphore
- Semaphore
- 类与对象
- android中的通信机制
- web.xml文件中引用log4J的配置以及log4J文件配置
- 2014年全球趣味科技大盘点
- Android中的时间自动更新
- Semaphore
- 代码抽象三原则!!
- MFC计算运行时间(opencv)
- android jni 内存泄露
- Android getSystemService()的API
- 如何联机调试和发布程序(99$)
- [Elasticsearch] 部分匹配 (二) - 通配符及正则表达式查询
- UIButton点击之后弹出UIDatePicker
- Linux errno 错误对照表