package cn.besteam.config;
|
|
import java.util.concurrent.Executors;
|
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.ThreadPoolExecutor;
|
import java.util.concurrent.TimeUnit;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.scheduling.annotation.EnableAsync;
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
@Configuration
|
@EnableAsync
|
public class ThreadPoolConfig {
|
|
@Bean(name = "customExecutor")
|
public ThreadPoolTaskExecutor customExecutor(){
|
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
|
threadPoolTaskExecutor.setCorePoolSize(30);
|
threadPoolTaskExecutor.setMaxPoolSize(200);
|
threadPoolTaskExecutor.setQueueCapacity(500);
|
threadPoolTaskExecutor.setKeepAliveSeconds(3);
|
threadPoolTaskExecutor.setThreadNamePrefix("custom-thread");
|
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
|
// CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行
|
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
|
//加载
|
threadPoolTaskExecutor.initialize();
|
return threadPoolTaskExecutor;
|
}
|
}
|