From 6b26bd24f52c96fc49ed9efca695ecf19087b593 Mon Sep 17 00:00:00 2001 From: chenhc <2369838784@qq.com> Date: Thu, 3 Apr 2025 16:10:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sale/admin/config/TaskPoolConfig.java | 72 ++++++++++++++----- .../config/ThreadPoolConfigProperties.java | 36 ++++++++++ .../src/main/resources/application.properties | 7 ++ 3 files changed, 98 insertions(+), 17 deletions(-) create mode 100644 api-admin/src/main/java/com/glxp/sale/admin/config/ThreadPoolConfigProperties.java diff --git a/api-admin/src/main/java/com/glxp/sale/admin/config/TaskPoolConfig.java b/api-admin/src/main/java/com/glxp/sale/admin/config/TaskPoolConfig.java index bd9ddb3..163aa43 100644 --- a/api-admin/src/main/java/com/glxp/sale/admin/config/TaskPoolConfig.java +++ b/api-admin/src/main/java/com/glxp/sale/admin/config/TaskPoolConfig.java @@ -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; } + } diff --git a/api-admin/src/main/java/com/glxp/sale/admin/config/ThreadPoolConfigProperties.java b/api-admin/src/main/java/com/glxp/sale/admin/config/ThreadPoolConfigProperties.java new file mode 100644 index 0000000..f2633b4 --- /dev/null +++ b/api-admin/src/main/java/com/glxp/sale/admin/config/ThreadPoolConfigProperties.java @@ -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_"; + + +} diff --git a/api-admin/src/main/resources/application.properties b/api-admin/src/main/resources/application.properties index e23c510..d28c92e 100644 --- a/api-admin/src/main/resources/application.properties +++ b/api-admin/src/main/resources/application.properties @@ -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 +