package com.zy.common.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.ThreadPoolExecutor; /** * Created by vincent on 2022/5/21 */ @Configuration @EnableAsync public class ThreadPoolConfig { @Bean(name = "orderThreadPool") public ThreadPoolTaskExecutor orderThreadPool() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); int core = Runtime.getRuntime().availableProcessors(); core = 2; //设置核心线程数 executor.setCorePoolSize(core); //设置最大线程数 executor.setMaxPoolSize(core * 10 + core); //除核心线程外的线程存活时间 executor.setKeepAliveSeconds(3); //缓冲队列 executor.setQueueCapacity(core); executor.setThreadNamePrefix("order-task-"); //设置拒绝策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); return executor; } }