ThreadPoolExecutor使用例
2093 ワード
public class Test1 {
private ThreadPoolExecutor threadpool;
/**
* Param:
* corePoolSize - , 。
* maximumPoolSize - ( LinkedBlockingQueue )。
* keepAliveTime - , , 。
* unit - keepAliveTime , : 。
* workQueue - ( )。 execute Runnable 。
* RejectedExecutionHandler - ( , execute() )
*/
public Test1(){
threadpool=new ThreadPoolExecutor(2, 10, 20, TimeUnit.SECONDS, new ArrayBlockingQueue(10),
new ThreadPoolExecutor.DiscardOldestPolicy());
}
//add task into thread pool
public void submit(final int flag){
threadpool.execute(new Runnable(){
public void run(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(flag + " Hello");
}
});
}
/**
* close thread pool
*/
public void shutdown() {
threadpool.shutdown();
}
public static void main(String[] args) {
Test1 t = new Test1();
for (int i = 0; i < 20; i++) {
System.out.println("time:" + i);
t.submit(i);
}
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
/**
* execute(Runnable) :
* 1. corePoolSize, , 。
* 2. corePoolSize, workQueue , 。
* 3. corePoolSize, workQueue , maximumPoolSize, 。
* 4. corePoolSize, workQueue , maximumPoolSize,
* handler 。 : : corePoolSize、 workQueue、 maximumPoolSize
* , , handler 。
*
* 5. corePoolSize , keepAliveTime, 。 , 。
*/
}