线程池配置

yml:文件

 

 创建一个spring 线程池配置类

@EnableAsync
@Configuration
public class ThreadPoolConfig {
    private static Logger logger = LoggerFactory.getLogger(ThreadPoolConfig.class);

    @Value("${threadpool.core-pool-size}")
    private int corePoolSize;

    @Value("${threadpool.max-pool-size}")
    private int maxPoolSize;

    @Value("${threadpool.queue-capacity}")
    private int queueCapacity;

    @Value("${threadpool.keep-alive-seconds}")
    private int keepAliveSeconds;


    @Bean(name = "asyncServiceExecutor")
    public Executor asyncServiceExecutor() {
        logger.info("start executor -->");
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置核心线程数
        executor.setCorePoolSize(corePoolSize);
        //设置最大线程数
        executor.setMaxPoolSize(maxPoolSize);
        //设置队列大小
        executor.setQueueCapacity(queueCapacity);
        //配置线程池的前缀
        executor.setThreadNamePrefix("async-plan-service-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //设置空闲时间
        executor.setKeepAliveSeconds(keepAliveSeconds);
        //进行加载
        executor.initialize();
        return executor;
    }
}

 

posted @ 2021-08-25 14:20  这很周锐  阅读(407)  评论(0编辑  收藏  举报