One, six states of Java threads
- 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.
- 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.
- 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.
- 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
- The thread pool's initialization state is running, capable of receiving new tasks, and processing the added tasks.
- 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.
- 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.
- 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.
- 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.
- 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