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