通過創(chuàng)建線程池執(zhí)行器的構(gòu)造方法去手動創(chuàng)建一個自己的線程池.
package ThreadPoll;
/*
創(chuàng)建一個線程池
corePoolSize=2,maximumPoolSize=5,keepAliveTime=1,keepAliveTime=TimeUnit.SECONDS
,workQueue=new LinkedBlockingQueue<Runnable>(3),threadFactory=Executors.defaultThreadFactory()
handler=new ThreadPoolExecutor.CallerRunsPolicy());
*/
import java.util.concurrent.*;
public class MyThreadPool {
public static void main(String[] args){
ExecutorService threadPool= //自定義線程池
new ThreadPoolExecutor(
2,
5,
1,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(3),//任務(wù)隊列容量設(shè)為3
Executors.defaultThreadFactory()
,new ThreadPoolExecutor.CallerRunsPolicy());//自定義拒絕策略
for(int i=1;i<=10;i++){
int d=i;
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+"辦理業(yè)務(wù)"+d);//模擬處理業(yè)務(wù)
});
}
}
}