业务代码篇......
gulimall:
thread:
coreSize: 50
max-Size: 200
keep-alive-time: 6000
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "gulimall.thread")
@Component
public class MyThreadConfigProperties {
private Integer coreSize;
private Integer maxSize;
private Integer keepAliveTime;
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Configuration
public class MyThreadConfig {
@Bean
public ThreadPoolExecutor threadPoolExecutor(MyThreadConfigProperties pool) {
return new ThreadPoolExecutor(pool.getCoreSize(), // 核心线程池的大小
pool.getMaxSize(), // 最大线程池的大小
pool.getKeepAliveTime(), // 空闲线程多长时间关闭
TimeUnit.SECONDS, // 时间单位
new LinkedBlockingQueue<>(10000), // 阻塞队列(队列长度)
Executors.defaultThreadFactory(), // 线程工厂
new ThreadPoolExecutor.AbortPolicy()); // 拒绝策略
}
}
原文:https://www.cnblogs.com/yanyaqiblog/p/13968666.html