import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
public class AsyncThreadPoolConfig {
// 核心线程数(默认线程数)
private static final int corePoolSize = 20;
// 最大线程数
private static final int maxPoolSize = 100;
// 允许线程空闲时间(单位:默认为秒)
private static final int keepAliveTime = 10;
// 缓冲队列大小
private static final int queueCapacity = 200;
// 线程池名前缀
private static final String threadNamePrefix = "Async-Service-";
@Bean("myTaskExecutor")
public ThreadPoolTaskExecutor myTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAliveTime);
executor.setThreadNamePrefix(threadNamePrefix);
// 设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
executor.setWaitForTasksToCompleteOnShutdown(true);
// 设置线程池中任务的等待时间,如果超过这个时候还没有销毁就强制销毁,以确保应用最后能够被关闭,而不是阻塞住.
executor.setAwaitTerminationSeconds(60);
// 线程池对拒绝任务的处理策略
// CallerRunsPolicy:由调用线程(提交任务的线程)处理该任务
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化
executor.initialize();
return executor;
}
}
转载请注明: 转载自 浮生一程
本文链接地址 SpringBoot 自定义线程池配置