大家好,又见面了,我是你们的朋友全栈君。
一、线程池配置
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;
}
/** * 发送短信线程池 * @return */
@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;
}
/** * 发送邮件线程池 * @return */
@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;
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136737.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...