当前位置:网站首页>线程池的状态
线程池的状态
2022-06-24 09:43:00 【文丑颜不良啊】
之前我们说过线程的六种状态以及每种状态之间的转换过程:线程的六种状态_文丑颜不良啊的博客-CSDN博客
这篇文章来说一下线程池的状态。
由源码可知,线程池的状态有如下几种:
public class ThreadPoolExecutor extends AbstractExecutorService {
/**
* The main pool control state, ctl, is an atomic integer packing
* two conceptual fields
* workerCount, indicating the effective number of threads
* runState, indicating whether running, shutting down etc
*
* In order to pack them into one int, we limit workerCount to
* (2^29)-1 (about 500 million) threads rather than (2^31)-1 (2
* billion) otherwise representable. If this is ever an issue in
* the future, the variable can be changed to be an AtomicLong,
* and the shift/mask constants below adjusted. But until the need
* arises, this code is a bit faster and simpler using an int.
*
* The workerCount is the number of workers that have been
* permitted to start and not permitted to stop. The value may be
* transiently different from the actual number of live threads,
* for example when a ThreadFactory fails to create a thread when
* asked, and when exiting threads are still performing
* bookkeeping before terminating. The user-visible pool size is
* reported as the current size of the workers set.
*
* The runState provides the main lifecycle control, taking on values:
*
* RUNNING: Accept new tasks and process queued tasks
* SHUTDOWN: Don't accept new tasks, but process queued tasks
* STOP: Don't accept new tasks, don't process queued tasks,
* and interrupt in-progress tasks
* TIDYING: All tasks have terminated, workerCount is zero,
* the thread transitioning to state TIDYING
* will run the terminated() hook method
* TERMINATED: terminated() has completed
*
* The numerical order among these values matters, to allow
* ordered comparisons. The runState monotonically increases over
* time, but need not hit each state. The transitions are:
*
* RUNNING -> SHUTDOWN
* On invocation of shutdown(), perhaps implicitly in finalize()
* (RUNNING or SHUTDOWN) -> STOP
* On invocation of shutdownNow()
* SHUTDOWN -> TIDYING
* When both queue and pool are empty
* STOP -> TIDYING
* When pool is empty
* TIDYING -> TERMINATED
* When the terminated() hook method has completed
*
* Threads waiting in awaitTermination() will return when the
* state reaches TERMINATED.
*
* Detecting the transition from SHUTDOWN to TIDYING is less
* straightforward than you'd like because the queue may become
* empty after non-empty and vice versa during SHUTDOWN state, but
* we can only terminate if, after seeing that it is empty, we see
* that workerCount is 0 (which sometimes entails a recheck -- see
* below).
*/
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
private static final int COUNT_BITS = Integer.SIZE - 3;
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
// runState is stored in the high-order bits
private static final int RUNNING = -1 << COUNT_BITS;
private static final int SHUTDOWN = 0 << COUNT_BITS;
private static final int STOP = 1 << COUNT_BITS;
private static final int TIDYING = 2 << COUNT_BITS;
private static final int TERMINATED = 3 << COUNT_BITS;
// Packing and unpacking ctl
private static int runStateOf(int c) { return c & ~CAPACITY; }
private static int workerCountOf(int c) { return c & CAPACITY; }
private static int ctlOf(int rs, int wc) { return rs | wc; }
/*
* Bit field accessors that don't require unpacking ctl.
* These depend on the bit layout and on workerCount being never negative.
*/
private static boolean runStateLessThan(int c, int s) {
return c < s;
}
private static boolean runStateAtLeast(int c, int s) {
return c >= s;
}
private static boolean isRunning(int c) {
return c < SHUTDOWN;
}
}| 状态 | 含义 |
|---|---|
| RUNNING | 运行状态:接受新任务,持续处理任务队列里的任务 |
| SHUTDOWN | 不再接受新任务,要处理任务队列里的任务 |
| STOP | 不再接受新任务,也不再处理任务队列里的任务,中断正在执行的任务 |
| TIDYING | 表示线程池正在停止运行,终止所有任务,销毁所有工作线程,当线程池执行 terminated() 方法时,线程池进入 TIDYING 状态 |
| TERMINATED | 表示线程池已停止运行,所有的工作线程已经被销毁,所有的任务已经被清空或者执行完毕,terminated() 方法执行完毕之后线程池进入此状态 |
根据源码中的注释可知,各个状态之间的转换流程为:

线程池的运行状态并不是由有湖显式设置的,而是伴随着线程池的运行有线程池自己内部去维护的。线程池内部使用一个变量 ctl 去维护线程状态(runState)和线程数量(workCount)两个值。
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));ctl 是一个 AtomicInteger 类型的变量,是对线程池的运行状态和线程池中有效线程数量进行控制的一个字段,一个字段同时包含两个含义。使用高 3 位来保存线程池的状态,低 29 位来保存线程池中有效线程的数量。一个变量存储两个值,两个值之间互不干扰,可避免在做出相关决策时出现不一致的情况。
本文参考自:线程池监控和动态配置 - 掘金
边栏推荐
- Getting user information for applet learning (getuserprofile and getUserInfo)
- Indexeddb local storage, homepage optimization
- 解决微信小程序rich-text富文本标签内部图片宽高自适应的方法
- PostgreSQL DBA quick start - source compilation and installation
- YOLOv6:又快又准的目标检测框架开源啦
- Symbol.iterator 迭代器
- MySQL data advanced
- 二叉樹第一部分
- Distributed | how to make "secret calls" with dble
- 学习使用KindEditor富文本编辑器,点击上传图片遮罩太大或白屏解决方案
猜你喜欢

Top issue tpami 2022! Behavior recognition based on different data modes: a recent review

Producer / consumer model

415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)

SQL Server AVG函数取整问题

2021-08-17

SQL Sever中的窗口函数row_number()rank()dense_rank()

CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance

Troubleshooting steps for Oracle pool connection request timeout

GeoGebra 实例 时钟

How large and medium-sized enterprises build their own monitoring system
随机推荐
卷妹带你学jdbc---2天冲刺Day1
请问有国内靠谱低手续费的期货开户渠道吗?网上开户安全吗?
Distributed | how to make "secret calls" with dble
PHP encapsulates a file upload class (supports single file and multiple file uploads)
Programming questions (continuously updated)
canvas管道动画js特效
el-table表格的拖拽 sortablejs
Binary tree part I
SQL Sever关于like操作符(包括字段数据自动填充空格问题)
Basic operations on binary tree
有关二叉树 的基本操作
JS proxy mode
dedecms模板文件讲解以及首页标签替换
oracle池式连接请求超时问题排查步骤
机器学习——主成分分析(PCA)
Jcim | AI based protein structure prediction in drug discovery: impacts and challenges
Symbol. Iterator iterator
indexedDB本地存储,首页优化
分布式 | 如何与 DBLE 进行“秘密通话”
保健品一物一码防窜货营销软件开发