springboot默认线程池的配置
#核心线程数
spring.task.execution.pool.core-size=8
#最大线程数
spring.task.execution.pool.max-size=16
#空闲线程存活时间
spring.task.execution.pool.keep-alive=60s
#是否允许核心线程超时
spring.task.execution.pool.allow-core-thread-timeout=true
#线程队列数量
spring.task.execution.pool.queue-capacity=100
#线程关闭等待
spring.task.execution.shutdown.await-termination=false
spring.task.execution.thread-name-prefix=task-
一般情况下,不会去修改配置参数,而是采用手动创建线程池的方式来处理。这样@async
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 手动创建线程池,
* 使用ThreadPoolTaskExecutor类进行配置
*/
@Configuration
public class PoolConfig {
@Bean
public TaskExecutor taskExecutor(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//设置核心线程数
executor.setCorePoolSize(10);
//设置最大线程数
executor.setMaxPoolSize(15);
//设置队列容量
executor.setQueueCapacity(20);
//设置线程活跃时间(秒)
executor.setKeepAliveSeconds(60);
//设置默认线程名称
executor.setThreadNamePrefix("smile-");
//设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
}
如果需要多个不同类型的线程池
@Configuration
public class PoolConfig {
@Bean("pool1")
public TaskExecutor taskExecutor(){
}
}
@Async("pool1")
网友评论