wvp-GB28181-pro/src/main/java/com/genersoft/iot/vmp/conf/ThreadPoolTaskConfig.java

69 lines
2.3 KiB
Java
Raw Normal View History

package com.genersoft.iot.vmp.conf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* ThreadPoolTask
* @author lin
*/
@Configuration
2021-11-17 17:01:59 +08:00
@EnableAsync(proxyTargetClass = true)
public class ThreadPoolTaskConfig {
2021-11-25 15:33:25 +08:00
public static final int cpuNum = Runtime.getRuntime().availableProcessors();
/**
* 线线线0线
* 线线corePoolSize
* 线线maxPoolSize使
*/
/**
* 线线
*/
2021-11-25 15:33:25 +08:00
private static final int corePoolSize = cpuNum;
/**
* 线
*/
2021-11-25 15:33:25 +08:00
private static final int maxPoolSize = cpuNum*2;
/**
* 线
*/
private static final int keepAliveTime = 30;
2022-05-11 18:37:24 +08:00
/**
*
*/
2022-05-11 18:37:24 +08:00
private static final int queueCapacity = 10000;
/**
* 线
*/
private static final String threadNamePrefix = "wvp-";
/**
*
* @return
*/
@Bean("taskExecutor") // bean的名称默认为首字母小写的方法名
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAliveTime);
executor.setThreadNamePrefix(threadNamePrefix);
// 线程池对拒绝任务的处理策略
// CallerRunsPolicy由调用线程提交任务的线程处理该任务
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化
executor.initialize();
return executor;
}
}