First create the thread pool MyThreadConfig:
@Configuration
public class MyThreadConfig {
@Bean
public ThreadPoolExecutor threadPoolExecutor(){
return new ThreadPoolExecutor(20,
200,10,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(100000),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
}
}
Then create the thread pool configuration class ThreadPoolConfigProperties:
@ConfigurationProperties(prefix = "gulimall.thread")
@Component
@Data
public class ThreadPoolConfigProperties {
//线程池核心大小
private Integer coreSize;
//最大大小
private Integer maxSize;
//休眠时长
private Integer keepAlive;
}
Here is gulimall.thread
to add dependencies to the prefix pom file in the configuration file
, which can automatically complete the configuration file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Related parameters can be configured in the configuration file:
After the configuration here is complete, you need to use a custom configuration class in MyThreadConfig:
Two methods:
First: Because ThreadPoolConfigProperties has been injected, use the annotation @EnableConfigurationProperties(ThreadPoolConfigProperties.class) directly on MyThreadConfig:
Second method:
set ThreadPoolConfigProperties passed in as a parameter: