线程池的创建示例
@Override
@Bean(name = "threadPool")
public ThreadPoolTaskExecutor getAsyncExecutor() {
ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
// 设置核心线程数
threadPool.setCorePoolSize(10);
// 设置最大线程数
threadPool.setMaxPoolSize(20);
// 线程池所使用的缓冲队列
threadPool.setQueueCapacity(10000);
// 等待任务在关机时完成--表明等待所有线程执行完
threadPool.setWaitForTasksToCompleteOnShutdown(true);
// 线程池关闭的等待时间(默认为0)
threadPool.setAwaitTerminationSeconds(60*5);
// 非核心线程空闲的存活时间
threadPool.setKeepAliveSeconds(1);
// 线程名称前缀
threadPool.setThreadNamePrefix("newton-thread-");
// 线程池拒绝策略
threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化线程
threadPool.initialize();
return threadPool;
}
线程池参数