浏览代码

优化线程池配置

648540858 10 月之前
父节点
当前提交
6f28e6b497

+ 1 - 0
src/main/java/com/genersoft/iot/vmp/conf/DynamicTask.java

@@ -34,6 +34,7 @@ public class DynamicTask {
         threadPoolTaskScheduler.setPoolSize(300);
         threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
         threadPoolTaskScheduler.setAwaitTerminationSeconds(10);
+        threadPoolTaskScheduler.setThreadNamePrefix("dynamicTask-");
         threadPoolTaskScheduler.initialize();
     }
 

+ 16 - 6
src/main/java/com/genersoft/iot/vmp/conf/ScheduleConfig.java

@@ -1,13 +1,18 @@
 package com.genersoft.iot.vmp.conf;
 
 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.annotation.SchedulingConfigurer;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.scheduling.config.ScheduledTaskRegistrar;
 
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.ThreadPoolExecutor;
 
+import static com.genersoft.iot.vmp.conf.ThreadPoolTaskConfig.cpuNum;
+
 /**
  * "@Scheduled"是Spring框架提供的一种定时任务执行机制,默认情况下它是单线程的,在同时执行多个定时任务时可能会出现阻塞和性能问题。
  * 为了解决这种单线程瓶颈问题,可以将定时任务的执行机制改为支持多线程
@@ -15,16 +20,21 @@ import java.util.concurrent.ThreadPoolExecutor;
 @Configuration
 public class ScheduleConfig implements SchedulingConfigurer {
 
-	public static final int cpuNum = Runtime.getRuntime().availableProcessors();
-
-	private static final int corePoolSize = cpuNum;
+	/**
+	 * 核心线程数(默认线程数)
+	 */
+	private static final int corePoolSize = Math.max(cpuNum, 20);
 
-	private static final String threadNamePrefix = "scheduled-task-pool-%d";
+	/**
+	 * 线程池名前缀
+	 */
+	private static final String threadNamePrefix = "schedule";
 
 	@Override
 	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
-		taskRegistrar.setScheduler(new ScheduledThreadPoolExecutor(corePoolSize,
+		ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(corePoolSize,
 				new BasicThreadFactory.Builder().namingPattern(threadNamePrefix).daemon(true).build(),
-				new ThreadPoolExecutor.CallerRunsPolicy()));
+				new ThreadPoolExecutor.CallerRunsPolicy());
+		taskRegistrar.setScheduler(scheduledThreadPoolExecutor);
 	}
 }

+ 4 - 7
src/main/java/com/genersoft/iot/vmp/conf/ThreadPoolTaskConfig.java

@@ -28,11 +28,11 @@ public class ThreadPoolTaskConfig {
     /**
      * 核心线程数(默认线程数)
      */
-    private static final int corePoolSize = cpuNum;
+    private static final int corePoolSize = Math.max(cpuNum * 2, 16);
     /**
      * 最大线程数
      */
-    private static final int maxPoolSize = cpuNum*2;
+    private static final int maxPoolSize = corePoolSize * 10;
     /**
      * 允许线程空闲时间(单位:默认为秒)
      */
@@ -45,12 +45,9 @@ public class ThreadPoolTaskConfig {
     /**
      * 线程池名前缀
      */
-    private static final String threadNamePrefix = "wvp-";
+    private static final String threadNamePrefix = "async-";
+
 
-    /**
-     *
-     * @return
-     */
     @Bean("taskExecutor") // bean的名称,默认为首字母小写的方法名
     public ThreadPoolTaskExecutor taskExecutor() {
         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();