线程池配置
一.配置方法:
1.在application.properties中添加
threadpool:
# 核心线程池数
corePoolSize: 8
#最大线程
maxPoolSize: 16
#队列容量
queueCapacity: 5
#空闲线程存活时间
keepAliveSeconds: 300
2.创建类ExecutePoolConfiguration
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
* 线程池配置
*
* @author
*
*/
@Configuration
@ConfigurationProperties(prefix = "threadpool")
public class ExecutePoolConfiguration {
@Value("${corePoolSize}")
private int corePoolSize;
@Value("${maxPoolSize}")
private int maxPoolSize;
@Value("${queueCapacity}")
private int queueCapacity;
@Value("${keepAliveSeconds}")
private int keepAliveSeconds;
@Bean(name = "threadPoolTaskExecutor")
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setKeepAliveSeconds(keepAliveSeconds);
pool.setCorePoolSize(corePoolSize); // 核心线程池数
pool.setMaxPoolSize(maxPoolSize); // 最大线程
pool.setQueueCapacity(queueCapacity); // 队列容量
pool.setRejectedExecutionHandler(new java.util.concurrent.Th