1
zj
2024-07-21 efa6ea80aba153fa07458baedd0d730505fe9665
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package org.example.mexcclient.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(100);    // 核心线程数, 根据需求进行调整
        executor.setMaxPoolSize(150);    // 最大线程数, 适当设置以避免资源耗尽
        executor.setQueueCapacity(200);    // 队列容量, 适当限制以避免请求堆积
        executor.setKeepAliveSeconds(30);    // 线程空闲时的存活时间为30秒,减少系统开销
        executor.setThreadNamePrefix("Thread-");    // 线程名称的前缀
 
        // 使用 CallerRunsPolicy 拒绝策略,以减少任务被拒绝时带来的负担
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
 
        // 初始化线程池,配置其他参数(不过可以根据需要添加)
        executor.initialize(); // 明确初始化,提升代码可读性
 
        return executor; // 返回配置好的线程池
    }
}