springboot默认线程池怎么配置
在Spring Boot中,可以通过配置文件(application.properties或application.yml)来配置默认的线程池。以下是一些常用的配置项:
- 核心线程数:
spring.task.execution.thread-pool.core-size=10
- 最大线程数:
spring.task.execution.thread-pool.max-size=20
- 队列容量:
spring.task.execution.thread-pool.queue-capacity=200
- 线程池名称前缀:
spring.task.execution.thread-name-prefix=my-thread-pool-
- 空闲线程存活时间:
spring.task.execution.thread-pool.keep-alive=60s
可以根据实际需求自行调整以上配置项的值。另外,如果需要自定义线程池,可以实现TaskExecutor
接口并在配置文件中进行配置。例如:
@Configuration
public class MyTaskExecutorConfig {
@Bean
public TaskExecutor myTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(200);
executor.setThreadNamePrefix("my-thread-pool-");
executor.setKeepAliveSeconds(60);
return executor;
}
}
然后在需要使用的地方注入TaskExecutor
并使用即可。
相关问答