java thread pool: Executor lifecycle
We know that threads have a variety of execution states, and the same thread pool that manages threads has several states. The JVM does not exit until all threads (non-background daemon threads) are terminated, and it is important to close a thread pool in order to conserve resources and effectively release resources. Sometimes failure to properly close the thread pool will prevent the end of the JVM.
The thread pool executor is an asynchronous execution task, so it is not possible to get the status of the submitted task directly at any time. These tasks may have been completed, may be executing, or are still queued for execution. So there are several things that can happen when you close a thread pool:
1. Smooth shutdown: All the tasks that have been started have been completed and no new tasks are accepted.
2. Close immediately: Cancel all tasks that are being performed and not performed
In addition, when the thread pool is closed, the status of the task should have corresponding feedback information.
Figure 1 depicts the 4 states of the thread pool.
1, the thread pool in the construction (new operation) is the initial state, once the construction completion of the thread pool into the execution state running. Strictly speaking, after the thread pool construction is complete, no threads are started immediately, and the thread is only started when a "pre-boot" or a task is received. This will be followed by a detailed analysis of the thread pool principle. However, the thread pool is running and ready to accept tasks for execution.
2, the thread pool can be run through shutdown () and Shutdownnow () to change the running state. Shutdown () is a gentle shutdown process in which the thread pool stops accepting new tasks while waiting for committed tasks to complete, including those that enter the queue that have not yet started, at which point the threads are in a shutdown state; Shutdownnow () is an immediate shutdown process, The thread pool stops accepting the new task, while the line pool cancels all tasks performed and the tasks that have entered the queue but have not yet been performed, at which point the threads pools are in a stop state.
3, once shutdown () or Shutdownnow () completed, the thread pool into the terminated state, at this time the thread pool is over.
IsTerminating () describes the shutdown and stop two states.
IsShutDown () describes the running state, which is shutdown/stop/terminated three states.
The API for the thread pool is as follows:
where Shutdownnow () returns a list of tasks that have entered the queue but have not yet been executed. Awaittermination describes the time that the wait thread pool is closed and throws a timeout exception if the wait time thread pool has not been closed.
A reject action is triggered for task submission that occurs during the shutdown of the thread pool. This is the task operation described by Java.util.concurrent.RejectedExecutionHandler. The next summary will describe the actions of these tasks when they are rejected.
Summary of this section:
1, the thread pool has run, shut down, stop, end four states, will be released after the end of all resources
2. Gently close the thread pool using shutdown ()
3, immediately close the thread pool using Shutdownnow (), and get the list of tasks not executed
4, detection thread pool is in the closed, using IsShutDown ()
5, detect whether the thread pool has been closed using isterminated ()
6, timed or permanent wait thread pool shutdown end using awaittermination () operation
The content of the thread pool lifecycle above comes from blogs: http://www.blogjava.net/xylz/archive/2011/01/04/342316.html.
The above does explain more clearly, but, oneself in see Threadpoolexecutor class source process, found that thread pool life cycle not only: RUNNING, SHUTDOWN, stop/terminated four states, There is also a tidying state.
In the Threadpoolexecutor class source code interception content is as follows:
* 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 N EW 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 * 'll run The terminated () Hook method * Terminated:terminated () has completed * * "numerical order among thes E values matters, to allow * ordered comparisons. The runstate monotonically increases over * time and but need not hit each state. The transitions are: * * RUNNING-> SHUTDOWN * on invocation of SHUTDOWN (), perhaps implicitly in fin
Alize () * (RUNNING or SHUTDOWN)-> STOP * on invocation of Shutdownnow () * SHUTDOWN-> tidying *When both queue and pool are empty * STOP-> tidying * When the pool is empty * tidying-> * When the terminated () Hooks method has completed * * Threads waiting in awaittermination () 'll return
When the * state reaches terminated. * Detecting the transition from SHUTDOWN to tidying are less * straightforward than you ' d like because the Queu E may become * empty after Non-empty and vice versa during SHUTDOWN state, but * we can only terminate if Seeing that it is empty, we are Workercount is 0 (which sometimes entails a recheck---
The above English is relatively simple, also relatively good understanding, here is not translated, according to the above meaning, that is, thread pool in shutdowm/stop to terminated state there is a tidying state. These changes are the status of this, here is a paste, interested can also go to the source to see.
* RUNNING-> SHUTDOWN
* invocation of SHUTDOWN (), perhaps implicitly in Finalize ()
* (RUNNING or Shutdow N)-> STOP
* invocation of Shutdownnow ()
* SHUTDOWN-> tidying * When both
queue and pool are Empty
* STOP-> tidying
* When pool is empty
* tidying --> terminated * when the Termin Ated () Hook method has completed
Summary
What we need to understand in this section is the lifecycle of the thread pool, and the status of the thread pool is reviewed below:
1, RUNNING
2, Shundown
3. STOP
4, tidying
5, terminated
The transformation relationships of these states are:
1. Call Shundown () method The state of the thread pool is determined by the Running-->shutdown
2. Call Shutdownow () method The state of the thread pool is determined by the Running-->stop
3, when the task queue and thread pool are empty when the state of the thread pool by stop/shutdown--–>tidying
4, when the terminated () method is called complete, the state of the thread pool is tidying ———->terminated State Reference
Blog: http://www.blogjava.net/xylz/archive/2011/01/04/342316.html.