Java thread and thread pool state

Source: Internet
Author: User
Tags visualvm

One, six states of Java threads

    1. such as 1,JDK defines that the thread state is not present in the "running" state, but for the convenience of describing the process some diagrams will draw a running state.
    2. After the Java thread is created, call the Start method to enter the ready state, run after the OS Dispatch is selected, and terminate when the program exits or throws an exception.
    3. Running the thread call Thread.yield () method state switches to a ready-to-run state, while running thread execution encounters IO blocking, calling Thread.Sleep or other thread object join () method to enter a blocking wait state The running thread encounters a lock pool wait state when the synchronization lock method is encountered.
    4. Threads from the blocking state, the lock pool state, the new state, the running state can return to the ready-to-run state when the relevant conditions are met, and then be checked into the running state by the OS scheduler.

Two, "VISUALVM thread monitoring thread state" and "Java thread State" correspondence relationship

When monitoring the JVM through VISUALVM, you can view the thread information of the JVM through the Threads tab, VISUALVM the thread status as follows:

Through the dump thread stack, and corresponding to the thread name in the VISUALVM monitoring information, find the VISUALVM thread stack for each thread state

Iii. five states of the Java thread pool

    1. The thread pool's initialization state is running, capable of receiving new tasks, and processing the added tasks.
    2. when the thread pool is in the shutdown state, it does not receive new tasks, but it can handle the tasks that have been added. when invoking the SHUTDOWN () interface of the thread pool, the thread pool is composed of running, SHUTDOWN.
    3. when the thread pool is in the stop state, it does not receive new tasks, does not handle the tasks that have been added, and interrupts the task being processed. when invoking the Shutdownnow () interface of the thread pool, the thread pool is composed of (RUNNING or SHUTDOWN), STOP.
    4. When all tasks are terminated, the CTL record has a "task number" of 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.
    5. 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.
    6. when the thread pool is completely terminated, it becomes a terminated state . When the thread pool is in the tidying state, after executing TERMINATED (), it will be TERMINATED by tidying.

Java thread and thread pool state

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.