Six states of threads
Multithreading may experience six states from creation to destruction
These six states can be explained in the source code of Java multithreading
public enum State { /** * Thread state for a thread which has not yet started. */ NEW, /** * Thread state for a runnable thread. A thread in the runnable * state is executing in the Java virtual machine but it may * be waiting for other resources from the operating system * such as processor. */ RUNNABLE, /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state is waiting for a monitor lock * to enter a synchronized block/method or * reenter a synchronized block/method after calling * {@link Object#wait() Object.wait}. */ BLOCKED, /** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: * <ul> * <li>{@link Object#wait() Object.wait} with no timeout</li> * <li>{@link #join() Thread.join} with no timeout</li> * <li>{@link LockSupport#park() LockSupport.park}</li> * </ul> * * <p>A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called <tt>Object.wait()</tt> * on an object is waiting for another thread to call * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on * that object. A thread that has called <tt>Thread.join()</tt> * is waiting for a specified thread to terminate. */ WAITING, /** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: * <ul> * <li>{@link #sleep Thread.sleep}</li> * <li>{@link Object#wait(long) Object.wait} with timeout</li> * <li>{@link #join(long) Thread.join} with timeout</li> * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> * </ul> */ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }
Translated as
NEW (creation status)
The thread state of the thread that has not been started, that is, the state when the thread is created and has not been called start().
RUNNABLE (running state)
The thread state of the runnable thread. A thread in a runnable state is executing in the Java virtual machine, but it may be waiting for other resources from the operating system, such as the processor. That is, the running state after calling the start method. However, it should be noted that when the start method is called, the state does not necessarily change immediately. The thread may still be making preparations, and the state at this time is uncertain.
BLOCKED (suspended)
Thread state of the thread that is blocked while waiting for the monitor to lock. The blocked thread is waiting for the monitor lock to enter the synchronization block / method or re-enter the synchronization block / method after the call
WAITING (unconditional WAITING state)
A thread enters the WAITING state because the following method is called:
- Object without time limit Wait method
- Thread without time limit Join method
- LockSupport.park
Then it will wait for other threads to perform a special action, such as: - An object that calls an object The thread of the wait method will wait for another thread to call the object of this object Notify() or object notifyAll().
- A called thread The thread of the join method will wait for the specified thread to end.
TIMED_WAITING (conditional waiting state)
The thread state of the waiting thread with the specified waiting time. The thread is in a timed wait state because one of the following methods is called with the specified positive wait time. That is, when the thread calls sleep (time), wait (time), join (time), locksupport Parknanos (time), locksupport The state after the method of parkuntil (time). If it is not awakened within the specified time or the waiting thread does not end, it will be awakened automatically by the system.
TERMINATED
Terminated thread status. That thread has completed execution. That is, the run method in the thread has been executed.
Diagram of transition between states
From the above, we know that there are six states in the thread life cycle, and we also know the meaning of thread state. In the life cycle of a thread, there is only one state at a time. The state of the thread can be obtained through the getState method of the thread.
The conversion diagram is as follows:
Blocking state
After carefully understanding the above article, we will think of a problem: BLOCKED, WAITING and TIMED_WAITING (conditional state) these three states seem to be the same, so what's the difference between them?
In fact, generally speaking, we can understand these three states as blocking states
We have the following categories of blocking states
- Wait for blocking
Wait for blocking, that is, object Wait(), when the thread runs this method, the JVM will put the thread into the waiting queue
- Synchronous blocking
Synchronization blocking is the Blocked state. When a running thread obtains the synchronization lock of an object, if the synchronization lock is occupied by another thread, the JVM will put the thread into the lock pool
- Other obstruction
Thread executes thread Sleep (time), or thread When using the join () method, or when an I/O request is issued, the JVM will turn the thread into a blocked state
When the sleep() state times out, the join() waits for the thread to terminate or time out, or the I/O processing is completed, the thread will return to the runnable state
The thread is blocking and waiting for monitor lock
A thread calls Object. when entering the critical area of synchronized modification, or in the synchronized critical region. Wait is then awakened to re-enter the synchronized critical region, which corresponds to the state.
You can refer to the following figure for understanding