优化线程池配置

pull/1719/head
648540858 2024-12-12 23:44:41 +08:00
parent 1b023da64f
commit 6f28e6b497
3 changed files with 21 additions and 13 deletions

View File

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

View File

@ -1,13 +1,18 @@
package com.genersoft.iot.vmp.conf; package com.genersoft.iot.vmp.conf;
import org.apache.commons.lang3.concurrent.BasicThreadFactory; 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.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import static com.genersoft.iot.vmp.conf.ThreadPoolTaskConfig.cpuNum;
/** /**
* "@Scheduled"Spring线 * "@Scheduled"Spring线
* 线线 * 线线
@ -15,16 +20,21 @@ import java.util.concurrent.ThreadPoolExecutor;
@Configuration @Configuration
public class ScheduleConfig implements SchedulingConfigurer { public class ScheduleConfig implements SchedulingConfigurer {
public static final int cpuNum = Runtime.getRuntime().availableProcessors(); /**
* 线线
*/
private static final int corePoolSize = Math.max(cpuNum, 20);
private static final int corePoolSize = cpuNum; /**
* 线
private static final String threadNamePrefix = "scheduled-task-pool-%d"; */
private static final String threadNamePrefix = "schedule";
@Override @Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(new ScheduledThreadPoolExecutor(corePoolSize, ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(corePoolSize,
new BasicThreadFactory.Builder().namingPattern(threadNamePrefix).daemon(true).build(), new BasicThreadFactory.Builder().namingPattern(threadNamePrefix).daemon(true).build(),
new ThreadPoolExecutor.CallerRunsPolicy())); new ThreadPoolExecutor.CallerRunsPolicy());
taskRegistrar.setScheduler(scheduledThreadPoolExecutor);
} }
} }

View File

@ -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的名称默认为首字母小写的方法名 @Bean("taskExecutor") // bean的名称默认为首字母小写的方法名
public ThreadPoolTaskExecutor taskExecutor() { public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();