在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接口并在配置文件中进行配置。例如:
@Configurationpublic 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并使用即可。

