线程池配置类
程序员文章站
2022-06-07 14:12:11
...
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* ScheduleConfig
* 线程池配置
*
**/
@Configuration
public class ScheduleConfig {
/**
* 线程池具体配置
*
* @param
* @return org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
* Copyright (c) 2021
* @Author chenkaimin
* @date 11:08 2021/5/24
**/
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
// 获取当前机器CPU核数
int cpuProcessors = Runtime.getRuntime().availableProcessors();
if (cpuProcessors == 0) {
cpuProcessors = 4;
}
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(cpuProcessors);
executor.setMaxPoolSize(cpuProcessors + 1);
// 线程池维护线程所允许的空闲时间
executor.setKeepAliveSeconds(60);
// 队列容量
executor.setQueueCapacity(1000);
// 等待任务执行完成在关闭
executor.setWaitForTasksToCompleteOnShutdown(true);
//该方法用来设置线程池中任务的等待时间,如果超过这个时候还没有销毁就强制销毁,以确保应用最后能够被关闭,而不是阻塞住。
executor.setAwaitTerminationSeconds(60);
// 线程前缀名称
executor.setThreadNamePrefix("async-service-");
// 配置拒绝策略:如果队列满了,继续往队列增加数据,则由调用线程处理该任务
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}