springboot配置线程池
时间: 2023-10-14 16:25:18 浏览: 38
在Spring Boot中配置线程池可以通过以下步骤完成:
1. 在pom.xml文件中添加依赖项:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
2. 在application.properties文件中添加线程池的属性值:
```
# 线程池的核心线程数
spring.task.execution.pool.core-size=10
# 线程池的最大线程数
spring.task.execution.pool.max-size=20
# 线程池的队列容量
spring.task.execution.pool.queue-capacity=2000
# 线程池的线程名称前缀
spring.task.execution.pool.thread-name-prefix=my-task-pool-
```
3. 创建一个ThreadPoolTaskExecutor bean:
```
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(2000);
executor.setThreadNamePrefix("my-task-pool-");
executor.initialize();
return executor;
}
```
4. 在需要使用线程池的地方注入bean:
```
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
```
这样就可以在Spring Boot中配置线程池了。