Java thread state overview
- New ( newly created )
- Runnable _ _ _
- Blocked _ _
- Waiting _ _ _
- Timed waiting _ _ _
- Terminated _ _ _
See the figure for details (source of figure: https://www.uml-diagrams.org/java-thread-uml-state-machine-diagram-example.html )
New New creation
Create a Thread object, but before calling start() to start the thread, the thread is in the initial state.
Runnable can run
In Java, the runnable state includes: Ready and Running
- Ready state
- The thread in this state has obtained all the resources required for execution, and the CPU can run as long as the execution right is allocated.
- All ready threads are stored in the ready queue.
- Running
- The thread that has obtained the CPU execution right and is being executed by the CPU.
- Since a CPU can only execute one thread at a time, each CPU has only one running thread at a time.
- CPU execution can be given up by yield (for example, in a non-preemptive operating system, the executing thread needs to actively give up the CPU)
Blocked is blocked
Note that it is a passive voice. When an executing thread fails to request a certain resource, it will enter a blocked state.
- In Java, the blocking state refers to the state that is forced to enter when the request for the lock fails. (Usually: locks, IO, Socket, etc. are all resources. But here only locks are involved)
- All blocked threads are stored in a blocking queue.
- A thread in the blocked state will continue to request resources. Once the request is successful, it will enter the Runnable-Ready ready queue and wait for execution.
Waiting
- When the wait, join, and park functions are called in the current thread, the current thread will enter the waiting state.
- There is also a waiting queue to store all waiting threads.
- A thread in the waiting state means that it needs to wait for instructions from other threads to continue running. Need to be woken up by other threads.
- Threads entering the waiting state will release CPU execution rights and release resources (such as locks)
Timed wating
- When the running thread calls sleep(time), wait, join, parkNanos, parkUntil, it will enter this state;
- It is the same as the waiting state, not because resources cannot be requested, but to enter actively.
- After entering this state, the CPU execution right and resources occupied are released.
- The difference from the waiting state: it can automatically enter Runnable-Ready after the set time
Terminated _
Including the natural termination of the operation, or the termination without catching an exception.
-----------------------------------------------------------------------------------------------------------
in addition:
Interrupt is a mechanism, which is a concept at a different level from the thread state here.
When the interrupt method is called on a thread, the thread's interrupt status is set. This is a boolean flag that every thread has. Each thread should check this flag from time to time (such as a while loop) to determine whether the thread is interrupted. However, if the thread is blocked, there is no way to detect the interrupt status. This is where the InterruptedExceptionioii exception is raised. When the interrupt method is called on a blocked thread (calling sleep or wait), the blocking call will be interrupted by an Interrupted Exception.