Java Multithreading Series--"Juc thread pool" 04 thread pool principle (iii)

Source: Internet
Author: User

This chapter describes the life cycle of the thread pool.

Threads have 5 states: New state, ready state, running state, blocking state, dead state. The thread pool also has 5 states; However, threads are different than threads, and the 5 states of the line pool are: Running, SHUTDOWN, STOP, tidying, TERMINATED.

The thread pool status definition code is as follows:

/** * The main pool control state, CTL, was an atomic integer packing * Both conceptual fields * Workercount, indicating the Effective number of threads * Runstate, indicating whether running, shutting down etc * on order to pack them into One int, we limit workercount to * (2^29)-1 (about million) threads rather than (2^31)-1 (2 * billion) otherwise repre Sentable. If This is ever a issue in * the future, the variable can being changed to being an atomiclong, * and the Shift/mask constants Below adjusted. But until the need * arises, this code was a bit faster and simpler using an int. * The Workercount are the number of work  ERS that has been * permitted to start and not permitted to stop. The value May is * 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 is still performing * bookkeeping before terminating. The user-visible pool size is * Reported as the current sizeof the workers set. * * The Runstate provides the main lifecycle control, taking on values: * * running:accept new tasks and process queue  D 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 has terminated, Workercount is zero, * The thread transitioning to state tidying * would run the terminated () Hook method * TERMINATE D:terminated () have completed * * The numerical order among these values matters, to allow * ordered comparisons. The runstate monotonically increases over * time, but need not hits each state. The transitions is: * * RUNNING-SHUTDOWN * on invocation of SHUTDOWN (), perhaps implicitly in Finalize () * (Runn  ING or SHUTDOWN), STOP * on invocation of Shutdownnow () * SHUTDOWN, tidying * when both queue and pool is Empty * STOP-tidying * When the pool is empty * TIdying-TERMINATED * When the TERMINATED () hook method had completed * * Threads waiting in awaittermination () wil L return when the * state reaches TERMINATED. * * Detecting the transition from SHUTDOWN to tidying are less * straightforward than you ' d like because the queue may Beco Me * Empty after non-empty and vice versa during SHUTDOWN state, but * we can only terminate if, after seeing then it is E  Mpty, we see * This workercount is 0 (which sometimes entails a recheck – see * below).*/Private FinalAtomicinteger ctl =NewAtomicinteger (Ctlof (RUNNING, 0));Private Static Final intCount_bits = integer.size-3;Private Static Final intCapacity = (1 << count_bits)-1;//runstate is stored in the High-order bitsPrivate Static Final intRUNNING =-1 <<count_bits;Private Static Final intSHUTDOWN = 0 <<count_bits;Private Static Final intSTOP = 1 <<count_bits;Private Static Final inttidying = 2 <<count_bits;Private Static Final intTERMINATED = 3 <<count_bits;//Packing and Unpacking ctlPrivate Static intRunstateof (intc) {returnC & ~capacity;}Private Static intWorkercountof (intc) {returnC &capacity;}Private Static intCtlof (intRsintWC) {returnRS | wc }

Description :
A CTL is an atomic object of type Atomicinteger. The CTL Records 2 information about the number of tasks in the thread pool and the thread pooling status.
The CTL consists of 32 bits in total. Where the high 3 bits represent the thread pool state, and the low 29 bits represent the number of tasks in the thread pool.

RUNNING    - The corresponding high 3-bit value is 111. SHUTDOWN   - The corresponding high 3-bit value is 000. STOP       - The corresponding high 3-bit value is 001. Tidying    ----the corresponding high 3-bit value is 011.

The switch between the various states of the thread pool is as follows:

1. RUNNING

(01) Status Note: When the thread pool is in the running state, it is able to receive new tasks and process the added tasks.
(02) State Toggle: The initialization state of the thread pool is running. In other words, once the thread pool is created, it is in the running state!
The reason is simple, in the initialization code of the CTL (below), it is initialized to the running state, and "task number" is initialized to 0.

Private Final New Atomicinteger (Ctlof (RUNNING, 0

2. SHUTDOWN

(01) Status Note: When the thread pool is in the shutdown state, it does not receive new tasks, but can handle the tasks that have been added.
(02) State Toggle: When invoking the SHUTDOWN () interface of the thread pool, the thread pool is running by SHUTDOWN.

3. STOP

(01) Status Description: When the thread pool is in the stop state, it does not receive new tasks, does not process the added tasks, and interrupts the task being processed.
(02) State toggle: When the Shutdownnow () interface of the thread pool is called, the thread pool is (RUNNING or SHUTDOWN), STOP.

4. Tidying
(01) Status Note: When all tasks are terminated, the number of tasks for the CTL record is 0, and the thread pool becomes tidying state. When the thread pool becomes tidying state, the hook function terminated () is executed. Terminated () is empty in the Threadpoolexecutor class and is handled appropriately if the user wants the online pool to be tidying, which can be implemented by overloading the terminated () function.
(02) State toggle: When the thread pool is in the SHUTDOWN state, the blocking queue is empty and the tasks performed in the thread pool are empty, it is SHUTDOWN-tidying.
When the thread pool is in the stop state, the task that is executed in the threads pools is empty, which is caused by stop---tidying.

5. TERMINATED
(01) Status Description: The thread pool is completely terminated and becomes terminated state.
(02) State toggle: When the thread pool is in tidying state, after executing TERMINATED (), it will be TERMINATED by tidying.

Reference:

Http://www.cnblogs.com/skywang12345/p/3509960.html

Java Multithreading Series--"Juc thread pool" 04 thread pool principle (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.