package com.xiaopeng.account.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(name = "couponSendExecutor") public ThreadPoolTaskExecutor couponSendExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); // 核心参数 executor.setCorePoolSize(5); executor.setMaxPoolSize(10); executor.setQueueCapacity(20); // 增大队列容量 executor.setKeepAliveSeconds(60); // 线程名称前缀 executor.setThreadNamePrefix("sendCoupon-"); // 拒绝策略:调用者线程执行 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 等待所有任务结束后再关闭线程池 executor.setWaitForTasksToCompleteOnShutdown(true); executor.setAwaitTerminationSeconds(60); // 初始化 executor.initialize(); return executor; } }