feat: 更新线程

dev
chenhc 3 days ago
parent c050d6f73f
commit 6b26bd24f5

@ -1,7 +1,9 @@
package com.glxp.sale.admin.config;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@ -10,29 +12,65 @@ import java.util.concurrent.ThreadPoolExecutor;
/**
* 线
*/
@Configuration
@EnableAsync
@Configuration
@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
public class TaskPoolConfig {
// @Bean(name = "taskExecutor")
// @Primary
// public ThreadPoolTaskExecutor taskExecutor() {
// // 获取当前主机的cpu核心数
// int threadCount = Runtime.getRuntime().availableProcessors();
// ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
// //核心池的大小
// taskExecutor.setCorePoolSize(threadCount);
// //线程池最大线程数
// taskExecutor.setMaxPoolSize(threadCount * 2);
// //队列最大长度
// taskExecutor.setQueueCapacity(200);
// //线程空闲时间
// taskExecutor.setKeepAliveSeconds(60);
// //配置线程前缀
// taskExecutor.setThreadNamePrefix("custom_executor");
// //配置拒绝策略
// taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// //执行初始化
// taskExecutor.initialize();
// return taskExecutor;
// }
@Bean(name = "taskExecutor")
public ThreadPoolTaskExecutor taskExecutor() {
// 获取当前主机的cpu核心数
int threadCount = Runtime.getRuntime().availableProcessors();
@Primary
public ThreadPoolTaskExecutor taskExecutor(ThreadPoolConfigProperties properties) {
// 动态获取CPU核心数
final int cpuCores = Runtime.getRuntime().availableProcessors();
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
//核心池的大小
taskExecutor.setCorePoolSize(threadCount);
//线程池最大线程数
taskExecutor.setMaxPoolSize(threadCount * 2);
//队列最大长度
taskExecutor.setQueueCapacity(200);
//线程空闲时间
taskExecutor.setKeepAliveSeconds(60);
//配置线程前缀
taskExecutor.setThreadNamePrefix("custom_executor");
//配置拒绝策略
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//执行初始化
// 核心线程数配置优先默认CPU核心数
taskExecutor.setCorePoolSize(
properties.getCorePoolSize() != null ?
properties.getCorePoolSize() : cpuCores
);
// 最大线程数配置优先默认CPU核心数*2
taskExecutor.setMaxPoolSize(
properties.getMaxPoolSize() != null ?
properties.getMaxPoolSize() : cpuCores * 2
);
// 其他固定配置项
taskExecutor.setQueueCapacity(properties.getQueueCapacity());
taskExecutor.setKeepAliveSeconds(properties.getKeepAliveSeconds());
taskExecutor.setThreadNamePrefix(properties.getThreadNamePrefix());
// 拒绝策略保持原样
taskExecutor.setRejectedExecutionHandler(
new ThreadPoolExecutor.CallerRunsPolicy()
);
taskExecutor.initialize();
return taskExecutor;
}
}

@ -0,0 +1,36 @@
package com.glxp.sale.admin.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
// 1. 创建配置类建议放在config包下
@ConfigurationProperties(prefix = "thread.pool")
@Data
public class ThreadPoolConfigProperties {
/**
* 线CPU
*/
private Integer corePoolSize;
/**
* 线CPU*2
*/
private Integer maxPoolSize;
/**
* 200
*/
private int queueCapacity = 200;
/**
* 线60
*/
private int keepAliveSeconds = 60;
/**
* 线"custom_executor_"
*/
private String threadNamePrefix = "custom_executor_";
}

@ -9,3 +9,10 @@ server.max-http-header-size=100MB
#\u6700\u5927\u4E0A\u4F20\u6587\u4EF6\u5927\u5C0F(10MB)
spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=200MB
# ?????????8?CPU???
thread.pool.core-pool-size= 8
thread.pool.max-pool-size= 16
thread.pool.queue-capacity= 1000
thread.pool.keep-alive-seconds= 160

Loading…
Cancel
Save