In layman's Java Concurrency (30): Thread pool Part 3 Executor life cycle [go]

Source: Internet
Author: User

We know that threads have multiple execution states, and the thread pool that manages threads also has multiple states. The JVM exits after all threads (non-background daemon threads) terminate, and it is important to close a thread pool in order to conserve resources and effectively release resources. Sometimes the thread pool cannot be shut down correctly, and the end of the JVM will be blocked.

The thread pool executor is an asynchronous execution task, so it is not possible to get the state of the submitted task directly at any moment. These tasks may have been completed, or they may be executing or are still queued for execution. So shutting down the thread pool can be a couple of things:

    • Smooth shutdown: All tasks that have been started are completed and no new tasks are accepted
    • Close now: Cancels all executing and non-performing tasks

Additionally, when the thread pool is closed, there should be feedback about the status of the task.

Figure 1 depicts the 4 states of the thread pool.

    • The thread pool before construction (new operation) is the initial state, once the construction completes the thread pool enters the execution state running. Strictly speaking, when the thread pool is constructed, no threads are started immediately, and the thread is started only when a "pre-boot" or a task is received. The principle behind the thread pool will be analyzed in detail. However, the thread pool is run and ready to accept tasks for execution.
    • The thread pool can be run with 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 the submitted tasks to complete, including those that have not yet started to enter the queue, at which point it is shutdown State; Shutdownnow () is an immediate shutdown process. The thread pool stops accepting the new task, while the wire pool cancels all the performed tasks and tasks that have entered the queue but have not yet performed, at which point the threads pools are in the stop state.
    • Once shutdown () or Shutdownnow () is executed, the thread pool enters the terminated state, and the thread pool ends.
    • IsTerminating () describes the shutdown and stop two states.
    • IsShutDown () describes the non-running state, which is shutdown/stop/terminated three states.

Figure 1

The API for the thread pool is as follows:

Figure 2

where Shutdownnow () returns a list of tasks that have entered the queue but have not yet been executed. Awaittermination describes the time it takes to wait for the thread pool to shut down, and a timeout exception will be thrown if the wait time thread pool is not closed.

A denial of action is triggered when a task submission occurs during the shutdown of a thread pool. This is the task action described by Java.util.concurrent.RejectedExecutionHandler. The next summary will describe the actions after these tasks are rejected.

Summarize this section:

    1. The thread pool has four states, run, close, stop, end, and all resources are released after the end
    2. Gentle shutdown thread pool using shutdown ()
    3. Immediately close the thread pool using Shutdownnow () and get the list of tasks that are not executed
    4. Detects if the thread pool is in the shutdown, using IsShutDown ()
    5. Detects if the thread pool has been turned off using isterminated ()
    6. Timed or permanently waits for thread pool shutdown to end with awaittermination () operation

In layman's Java Concurrency (30): Thread pool Part 3 Executor life cycle [go]

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.