线程池应该已经有很多人总结了,记录一下简单的参数验证:
public class ThreadPool {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 60,
TimeUnit.SECONDS, new LinkedBlockingDeque<>(3), new CustomizableThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
for (int i=0; i<20; i++) {
final int id = i;
System.out.println("threadPool alive Count si : " + threadPoolExecutor.getActiveCount() );
threadPoolExecutor.execute(new Thread(()->{
try {
System.out.println("Current Tread name is : " + Thread.currentThread().getName() + "/ ThreadNum is " + id) ;
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}));
}
}
}
运行截图:
参数说明:
1.核心线程数5,当存活线程为5,
2.之后来的任务塞队列,这里设置大小3方便查看,
3.塞满队列,开始new新的线程,直到超过最大线程数;
4.拒绝策略,这里默认用的是超过最大抛异常;
----to be continued