温馨提示×

springboot默认线程池怎么配置

小亿
144
2023-10-27 01:51:15
栏目: 编程语言

在Spring Boot中,可以通过配置文件(application.properties或application.yml)来配置默认的线程池。以下是一些常用的配置项:

  1. 核心线程数:spring.task.execution.thread-pool.core-size=10
  2. 最大线程数:spring.task.execution.thread-pool.max-size=20
  3. 队列容量:spring.task.execution.thread-pool.queue-capacity=200
  4. 线程池名称前缀:spring.task.execution.thread-name-prefix=my-thread-pool-
  5. 空闲线程存活时间: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并使用即可。

0