Thread state
Thread states can be divided into 6 states or 7 states, the specific states are as follows
Six states
- New: indicates the New state
- Runnable: Indicates the running status
- Terminated: indicates the Terminated state
- Waiting: indicates the Waiting state
- TimeWaiting: Indicates the timeout waiting state
- Blocked: the virtual drive is Blocked
7 state
- New: indicates the New state
- Ready: Indicates the Ready state
- Running: Indicates the Running status
- Terminated: indicates the Terminated state
- Waiting: indicates the Waiting state
- TimeWaiting: Indicates the timeout waiting state
- Blocked: the virtual drive is Blocked
The difference between:
The Runnable state in the 6 states is divided into Ready state and Running state.
New State (New)
You can declare a Thread class by implementing the Runnable interface or by inheriting Thread. When an instance is new, the Thread enters the new state.
A newly created thread is in the new state
Ready state
After the thread object is successfully created, the start() method of the thread is called, and the thread enters the Ready state. The thread in this state enters the runnable thread pool and waits to acquire the CPU.
Running Status
The thread scheduler is selecting a thread from the runnable thread pool and the thread is in the Running state. That is, the thread fetched the CPU’s time slice.
When the time slice runs out or the thread’s yield() method is invoked, the thread becomes Ready again
Terminated state (Terminated)
The thread continues to run until execution ends or is Terminated unexpectedly due to an exception.
Waiting state
When a Running thread executes wait(), join(), or locksupport.park (), the thread enters a Waiting state.
The wait() and joint() methods tell the JVM to put the thread into a lock wait queue. A thread in this state is not allocated CPU execution time and must wait to be awakened, otherwise it will remain in a Waiting state.
Wake up the thread
Unpark (Thread Thread) to wake up the specified Thread, which returns to the ready state.
The notify(), notifyAll(), and join completion modes wake up the thread waiting for the lock queue, and the thread leaving the queue returns to the ready state.
Timed wait status
Timed Waiting state is the same as Waiting state, the only difference is that there is a timeout mechanism. Instead of Waiting to be actively woken up by another thread, the thread is automatically woken up after reaching a specified time
The following method causes the thread to enter a Timed Waiting state
- wait(long)
- join(long)
- LockSupport.parkNanos(long)
- LockSupport.parkUtil(long)
- sleep(long)
The wait(long) and Join (long) methods cause the JVM to place threads on lock queues
The state of being Blocked
When a running thread fails to acquire a synchronization lock or issues an I/O request, the thread is Blocked.
If the lock fails to be acquired, the JVM also places the thread in the lock’s synchronization queue.
When a synchronization lock is released, the synchronization queue of the lock exits all threads and enters the ready state.
After I/O processing is complete, the thread returns to the ready state.