package org.example.kucoinclient.ThreadConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.ThreadPoolExecutor; /** * @program: dabaogp * @description: 线程池配置 * @create: 2024-06-25 16:37 **/ @Configuration public class AsyncConfiguration { @Bean(name = "threadPoolTaskExecutor") public ThreadPoolTaskExecutor threadPoolTaskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(50); // 核心线程数, 根据需求进行调整 executor.setMaxPoolSize(100); // 最大线程数, 适当设置以避免资源耗尽 executor.setQueueCapacity(150); // 队列容量, 适当限制以避免请求堆积 executor.setKeepAliveSeconds(30); // 线程空闲时的存活时间为30秒,减少系统开销 executor.setThreadNamePrefix("Thread-"); // 线程名称的前缀 // 使用 CallerRunsPolicy 拒绝策略,以减少任务被拒绝时带来的负担 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化线程池,配置其他参数(不过可以根据需要添加) executor.initialize(); // 明确初始化,提升代码可读性 return executor; // 返回配置好的线程池 } }