semaphoretest.java
来自「用NETBEANS做的一个关于Java的小小的demo.大家赐教」· Java 代码 · 共 45 行
JAVA
45 行
/*
* 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 + =
减小字号Ctrl + -
显示快捷键?