Spring Boot 配置和使用多线程池的实现
作者:阿懒土灵 时间:2022-09-04 19:53:02
某些情况下,我们需要在项目中对多种任务分配不同的线程池进行执行。从而通过监控不同的线程池来控制不同的任务。为了达到这个目的,需要在项目中配置多线程池。
spring boot 提供了简单高效的线程池配置和使用方案。
配置
首先是配置线程池的bean交给spring 管理:
@Configuration
public class TaskExecutePool {
@Bean(name ="threadPoolA")
public ThreadPoolTaskExecutormyTaskAsyncPool() {
ThreadPoolTaskExecutor executor =new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setMaxPoolSize(8);
executor.setQueueCapacity(100);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("Pool-A");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
@Bean(name ="ThreadPoolB")
public ThreadPoolTaskExecutorAsyncPoolB() {
ThreadPoolTaskExecutor executor =new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(4);
executor.setQueueCapacity(8);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("Pool-B");
//当任务数量超过MaxPoolSize和QueueCapacity时使用的策略,该策略是又调用任务的线程执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
使用
使用线程只需要在执行方法上加上注释,同时该方法的类必须被定义为bean,交由spring管理。
可以在类上使用注解@Component、@Service等
@Async(value="ThreadPoolA")
public void taskA(){
...
}
查看线程活跃数:
@Autowired
private ThreadPoolTaskExecutor threadPoolA;//变量名称为定义的线程池bean定义的name属性名。
public void checkAvtiveThreadNum() {
int num = threadPoolA.getActiveCount();
}
当然还有其他一些方法,这里不再举例。
线程池各属性理解:
corePoolSize:表示线程池核心线程,正常情况下开启的线程数量。
queueCapacity:当核心线程都在跑任务,还有多余的任务会存到此处。
maxPoolSize:如果queueCapacity存满了,还有任务就会启动更多的线程,直到线程数达到maxPoolSize。如果还有任务,则根据拒绝策略进行处理。
拒绝策略有多种:
由任务调用线程执行
抛异常
多余的直接抛弃
根据FIFO(先进先出)抛弃队列里任务
来源:https://www.jianshu.com/p/0170d71dc502
标签:Spring,Boot,多线程池
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java如何根据不同系统动态获取换行符和盘分割符
2022-02-27 10:33:10
JSON.toJSONString()空字段不忽略修改的问题
2023-06-16 03:12:37
使用反射方式获取JPA Entity的属性和值
2023-07-24 17:43:22
SpringBoot如何使用自定义注解实现接口限流
2023-11-25 07:22:37
![](https://img.aspxhome.com/file/2023/0/60110_0s.png)
如何在Android中实现左右滑动的指引效果
2023-06-23 09:08:47
![](https://img.aspxhome.com/file/2023/8/82398_0s.png)
详解Alibaba Java诊断工具Arthas查看Dubbo动态代理类
2021-08-04 03:16:28
![](https://img.aspxhome.com/file/2023/4/64064_0s.png)
Java求最小生成树的两种算法详解
2023-11-10 07:21:24
![](https://img.aspxhome.com/file/2023/6/59246_0s.png)
JAVA中Comparable接口和自定义比较器示例讲解
2023-11-20 22:16:32
Springboot文件上传功能的实现
2021-09-20 12:48:47
![](https://img.aspxhome.com/file/2023/0/64400_0s.png)
slf4j jcl jul log4j1 log4j2 logback各组件系统日志切换
2023-08-08 13:00:41
![](https://img.aspxhome.com/file/2023/8/57928_0s.png)
java基础的详细了解第九天
2022-11-11 20:53:50
![](https://img.aspxhome.com/file/2023/0/64950_0s.png)
JAVA基础之一些不为人知的那些秘密
2023-11-29 07:49:29
![](https://img.aspxhome.com/file/2023/8/59748_0s.jpg)
Java jpa外连接查询join案例详解
2022-12-17 18:31:15
springboot配置文件绑定实现解析
2022-06-07 23:32:38
![](https://img.aspxhome.com/file/2023/7/61917_0s.png)
Java 详解包装类Integer与int有哪些共通和不同
2022-06-15 20:12:18
Java简单计时的实现案例(可以用来限时循环)
2023-02-01 21:39:19
Flutter路由传递参数及解析实现
2023-06-22 11:48:45
![](https://img.aspxhome.com/file/2023/5/90155_0s.gif)
详解如何更改SpringBoot TomCat运行方式
2021-11-17 02:48:01
![](https://img.aspxhome.com/file/2023/6/61606_0s.png)
java搭建ftp/sftp进行数据传递的全过程
2023-11-29 15:08:10
![](https://img.aspxhome.com/file/2023/6/60486_0s.png)
Java Code Cache满导致应用性能降低问题解决
2021-06-18 18:17:56