📄 semaphoretest.java
字号:
/*
* SemaphoreTest.java
*
* Created on 2007年9月14日, 上午11:43
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package semaphore;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
* @author Administrator
*/
public class SemaphoreTest {
public static void main(String[] args){
final Pool aPool = new Pool(2);
Runnable worker = new Runnable() {
public void run() {
String resource = null;
try {
//取得resource
resource = aPool.get();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
//用resource做工作
System.out.println("I worked on "+resource);
//归还resource
aPool.put(resource);
}
};
ExecutorService service = Executors.newCachedThreadPool();
for(int i=0; i<20; i++){
service.submit(worker);
}
service.shutdown();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -