springboot中线程池配置

配置类ThreadPoolConfig

@EnableAsync

@Configuration

public class ThreadPoolConfig{

@Bean

public Executor asyncServiceExecutor(){

    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

    executor.setCorePoolSize(10);//配置核心线程数

    executor.setMaxPoolSize(15);//配置最大线程数

    executor.setQueueCapacity(50);配置队列大小

    executor.setThreadNamePrefix("threadpool-servece-");//线程的名称前缀

    //Caller_runs:不在新线程中执行任务,而是在调用者所在线程执行

    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

    executor.initialize();

    return executor;

}

 

}

 

然后在方法上加上@Async;多用在定时任务和下载图表的方法上

 

 

你可能感兴趣的:(springboot中线程池配置)