共计 1437 个字符,预计需要花费 4 分钟才能阅读完成。
RUNNING
能接管新工作并解决已增加的工作
线程池一旦创立,就处于 RUNNING 状态,此时线程池中的工作数为 0
SHUTDOWN
不接管新工作,但能解决已增加的工作
调用线程池的 shutdown()接口,线程池由 RUNNING》SHUTDOWN
STOP
线程池处于 STOP 状态,不接管新工作,不解决已增加的工作,并且会中断正在解决的工作
调用线程池的 shutdownNow()接口,线程池由 RUNNING or SHUTDOWN》STOP
TIDYING
当所有工作已终止,ctl 记录的工作数为 0,线程池的状态会变为 TIDYING
当线程池的状态变为 TIDYING 状态时,会调用钩子函数 terminated(),该办法在 ThreadPoolExecutor 中是空的,若用户想在线程池变为 TIDYING 时进行相应的解决,就须要重载 terminated()函数
当线程池状态为 SHUTDOWN,阻塞队列为空,线程池中执行的工作也为空时,由 SHUTDOWN》TIDYING
当线程池状态为 STOP,线程池中执行的工作为空时,由 STOP》TIDYING
TERMINATED
线程池彻底终止
线程池处于 TIDYING 状态,调用 terminated()时,由 TIDYING》TERMINATED
ThreadPoolExecutor 源码正文
* 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.
正文完