package com.simuwang.base.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; @Configuration @EnableAsync public class ThreadPoolConfig { @Bean("valuationExecutor") public ThreadPoolTaskExecutor valuationExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); int cpuSize = Runtime.getRuntime().availableProcessors(); cpuSize = Math.max(cpuSize/2, 1); taskExecutor.setCorePoolSize(cpuSize); taskExecutor.setMaxPoolSize(50); taskExecutor.setQueueCapacity(1024 * 4); taskExecutor.setKeepAliveSeconds(60); taskExecutor.setThreadNamePrefix("valuationExecutor--"); taskExecutor.setWaitForTasksToCompleteOnShutdown(true); taskExecutor.setAwaitTerminationSeconds(60); // 修改拒绝策略为使用当前线程执行 taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化线程池 taskExecutor.initialize(); return taskExecutor; } @Bean("asyncExecutor") public ThreadPoolTaskExecutor asyncExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(2); taskExecutor.setMaxPoolSize(50); taskExecutor.setQueueCapacity(50); taskExecutor.setKeepAliveSeconds(60); taskExecutor.setThreadNamePrefix("asyncExecutor--"); taskExecutor.setWaitForTasksToCompleteOnShutdown(true); taskExecutor.setAwaitTerminationSeconds(60); // 修改拒绝策略为使用当前线程执行 taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化线程池 taskExecutor.initialize(); return taskExecutor; } }