Java multi-thread series-Principle of thread pool in "JUC thread pool" 04 (III)

Source: Internet
Author: User

This chapter describes the lifecycle of a thread pool. In the "Java multi-thread series-" basic articles "01 Basic Concepts", we have introduced five States of threads: new state, ready state, running state, and blocking state, death status. The thread pool also has five States. However, the thread pool is different from the thread pool in five states: Running, SHUTDOWN, STOP, TIDYING, and TERMINATED. The thread pool status definition code is as follows: copy the code 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; 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; private static int ctlOf (int rs, int wc) {return rs | wc;} copy the Code Description: ctl is an atomic object of the AtomicInteger type. Ctl records two information: "number of tasks in the thread pool" and "Thread Pool status. Ctl contains 32 bits. Three digits in height indicate the thread pool status, and 29 digits in height indicate the number of tasks in the thread pool ". RUNNING -- the corresponding 3-bit high value is 111. SHUTDOWN -- the 3-bit high value is 000. STOP -- the 3-bit high value is 001. TIDYING -- the corresponding high 3-bit value is 010. TERMINATED -- the 3-bit high value is 011. Shows the switching between various States of the thread pool: 1. RUNNING (01) State Description: When the thread pool is in the RUNNING state, it can receive new tasks and process the added tasks. (02) status switching: the initialization status of the thread pool is RUNNING. In other words, the thread pool is in the RUNNING state once it is created! The principle is very simple. In the ctl initialization code (as shown below), it will be initialized to the RUNNING state, and the "number of tasks" will be initialized to 0. Private final AtomicInteger ctl = new AtomicInteger (ctlOf (RUNNING, 0); 2. SHUTDOWN (01) status description: When the thread pool is in the SHUTDOWN status, it does not receive new tasks, but can process the added tasks. (02) status switch: When the shutdown () interface of the thread pool is called, the thread pool is started from RUNNING to SHUTDOWN. 3. STOP (01) status description: When the thread pool is in the STOP State, it does not receive new tasks, does not process added tasks, and interrupts the tasks being processed. (02) status switch: When the shutdownNow () interface of the thread pool is called, the thread pool starts from (RUNNING or SHUTDOWN) to> STOP. 4. TIDYING (01) status description: When all tasks have been terminated, the "number of tasks" recorded by ctl is 0, and the thread pool changes to the TIDYING status. When the thread pool changes to the TIDYING status, the hook function terminated () is executed (). Terminated () is empty in the ThreadPoolExecutor class. If you want to handle it when the thread pool changes to TIDYING, you can use the overload terminated () function. (02) status switching: When the thread pool is in the SHUTDOWN state, the tasks in the blocking queue are empty and the execution in the thread pool is empty, it will be SHUTDOWN-> TIDYING. When the thread pool is in the STOP State and the task executed in the thread pool is empty, it will be stopped-> TIDYING. 5. TERMINATED (01) status description: When the thread pool is completely TERMINATED, it becomes TERMINATED. (02) state switch: When the thread pool is in the TIDYING state, after terminated () is executed, it will be TIDYING-> TERMINATED.

Related Article

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.