ThreadPoolTaskExecutor线程池参数配置
ThreadPool
A simple C++11 Thread Pool implementation
项目地址:https://gitcode.com/gh_mirrors/th/ThreadPool
免费下载资源
·
一、线程池配置
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;
}
}
2、使用@Async注解的问题
如果想要要结合Spring提供的异步注解@Async使用,需要在@Async注解中指定我们自定义的线程池的Bean。不指定我们自定义线程池的Bean的话将会默认使用Spring提供的线程池,默认情况下,每次都会创建一个新线程,如果在高并发的场景下,可能会产生大量的线程,从而导致OOM问题。
所以建议大家在@Async注解开启的异步功能时,请别忘了定义一个线程池,同时指明要生效的线程池的Bean。
如:
GitHub 加速计划 / th / ThreadPool
7.74 K
2.22 K
下载
A simple C++11 Thread Pool implementation
最近提交(Master分支:2 个月前 )
9a42ec13 - 9 年前
fcc91415 - 9 年前
更多推荐
已为社区贡献17条内容
所有评论(0)