一、线程池配置
1、ThreadPoolConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
public class ThreadPoolConfig {
private final static int AVAILABLE_PROCESSOR = Runtime.getRuntime().availableProcessors();
@Bean("taskPoolExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(6);
taskExecutor.setMaxPoolSize(6*2);
taskExecutor.setQueueCapacity(12);
taskExecutor.setKeepAliveSeconds(60);
taskExecutor.setThreadNamePrefix("taskPoolExecutor--");
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
taskExecutor.setAwaitTerminationSeconds(60);
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
taskExecutor.initialize();
return taskExecutor;
}
@Bean("sendSmsThreadPool")
public Executor sendSmsThreadPool() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(AVAILABLE_PROCESSOR);
taskExecutor.setMaxPoolSize(AVAILABLE_PROCESSOR + 1);
taskExecutor.setQueueCapacity(256);
taskExecutor.setKeepAliveSeconds(20);
taskExecutor.setThreadNamePrefix("sendSmsThreadPool--");
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
taskExecutor.setAwaitTerminationSeconds(60);
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
taskExecutor.initialize();
return taskExecutor;
}
@Bean("sendEmailThreadPool")
public Executor sendEmailThreadPool() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(AVAILABLE_PROCESSOR);
taskExecutor.setMaxPoolSize(AVAILABLE_PROCESSOR + 1);
taskExecutor.setQueueCapacity(256);
taskExecutor.setKeepAliveSeconds(20);
taskExecutor.setThreadNamePrefix("sendEmailThreadPool--");
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
taskExecutor.setAwaitTerminationSeconds(60);
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
taskExecutor.initialize();
return taskExecutor;
}
@Bean(name = "threadPoolTaskExecutor")
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(5);
executor.setQueueCapacity(10);
executor.setKeepAliveSeconds(300);
executor.setThreadNamePrefix("thread-ayokredit");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}
|