当前位置:网站首页>Multi-threaded ThreadPoolExecutor
Multi-threaded ThreadPoolExecutor
2022-08-11 02:24:00 【Seven kingdoms of the world, I want ninety-nine】
The relationship class diagram is:
1 线程池状态
ThreadPoolExecutor 使用 int 的高 3 位来表示线程池状态,低 29 位表示线程数量
状态名 | 高3位 | 接收新任务 | 处理阻塞队列任务 | 说明 |
---|---|---|---|---|
RUNNING | 111 | Y | Y | |
SHUTDOWN | 000 | N | Y | 不会接收新任务,但会处理阻塞队列剩余 任务 |
STOP | 001 | N | N | 会中断正在执行的任务,并抛弃阻塞队列 任务 |
TIDYING | 010 | 任务全执行完毕,活动线程为 0 即将进入 终结 | ||
TERMINATED | 011 | 终结状态 |
从数字上比较,TERMINATED > TIDYING > STOP > SHUTDOWN > RUNNING
Information is stored in an atomic variable ctl 中,目的是将线程池状态与线程个数合二为一,这样就可以用一次 cas 原子操作 进行赋值
// c 为旧值, ctlOf 返回结果为新值
ctl.compareAndSet(c, ctlOf(targetState, workerCountOf(c))));
// rs 为高 3 位代表线程池状态, wc 为低 29 位代表线程个数,ctl 是合并它们
private static int ctlOf(int rs, int wc) {
return rs | wc; }
2 构造方法
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
/* 参数说明: corePoolSize 核心线程数目 maximumPoolSize 最大线程数目 keepAliveTime 生存时间 unit 时间单位 workQueue 阻塞队列 threadFactory 线程工厂 - 可以为线程创建时起个好名字 handler 拒绝策略 */
工作方式:
线程池中刚开始没有线程,当一个任务提交给线程池后,线程池会创建一个新线程来执行任务
当线程数达到 corePoolSize 并没有线程空闲,这时再加入任务,新加的任务会被加入workQueue 队列排 队,直到有空闲的线程
如果队列选择了有界队列,那么任务超过了队列大小时,会创建 maximumPoolSize - corePoolSize 数目的线 程来救急
如果线程到达 maximumPoolSize 仍然有新任务这时会执行拒绝策略.拒绝策略 jdk 提供了 4 种实现,其它 Frameworks also provide different implementations
- jdk
- AbortPolicy 让调用者抛出 RejectedExecutionException 异常,默认策略
- CallerRunsPolicy 让调用者运行任务
- DiscardPolicy 放弃本次任务
- DiscardOldestPolicy 放弃队列中最早的任务,本任务取而代之
- Dubbo 的实现,在抛出 RejectedExecutionException 异常之前会记录日志,并 dump 线程栈信息,方 便定位问题
- Netty 的实现,是创建一个新线程来执行任务
- ActiveMQ 的实现,带超时等待(60s)尝试放入队列
- PinPoint 的实现,它使用了一个拒绝策略链,会逐一尝试策略链中每种拒绝策略 (Similar to chain of responsibility)
- jdk
当高峰过去后,超过corePoolSize 的救急线程如果一段时间没有任务做,需要结束节省资源,这个时间由 keepAliveTime 和 unit 来控制.
Denial Policy Class Diagram
边栏推荐
- 软件测试面试题:在频繁的版本发布中,如何回归测试?
- 【C 数据存储详解】(1)——深度剖析整形数据在内存中的存储
- TRCX: doping process analysis
- Oops Framework模板项目新手引导
- Inter-process communication method (2) Named pipe
- sql 使用到where和groupby时建立索引结果为啥是这样,原理是什么?
- Ninjutsu_v3_08_2020-安全渗透系统安装
- comp3331-9331-16s2-midterm复习
- ARM development (4) How to read the chip manual for novice Xiaobai, bare metal driver development steps and pure assembly to achieve lighting, assembly combined with c lighting, c to achieve lighting
- 英伟达 GPU 架构简史
猜你喜欢
随机推荐
Section 4-6 of the first week of the second lesson: Appreciation of medical prognosis cases + homework analysis
软件测试面试题:在频繁的版本发布中,如何回归测试?
How to solve the problem of Tomcat booting and crashing
通过微透镜阵列的传播
Pytorch/TensorFlow/Numpy常用函数汇总
PIFuHD配置记录
深度学习中的模型设计
Please talk about for...in and for...of in JS (below)
通过热透镜聚焦的高斯光束
ES进阶 数组功能语法新特性详解
ARM development (4) How to read the chip manual for novice Xiaobai, bare metal driver development steps and pure assembly to achieve lighting, assembly combined with c lighting, c to achieve lighting
Mysql_Note3
js原型和原型链及原型继承
夫妻一方婚内向异性大额转款,怎么判
软件测试面试题:测试用例与测试脚本?
redis学习五redis的持久化RDB,fork,copyonwrite,AOF,RDB&AOF混合使用
TRCX: doping process analysis
google搜索技巧——程序员推荐
第二课第一周第4-6节 医学预后案例欣赏+作业解析
【PHP】入门知识