執行緒池配置
阿新 • • 發佈:2021-08-25
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}") privateint 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; } }