The life cycle of a thread and its six state transitions

Six states of threads

The life cycle of a thread mainly has the following six states:

  • New (Newly Created)
  • Runnable (runnable)
  • Blocked
  • Waiting
  • Timed Waiting
  • Terminated

In our program coding, if we want to determine the current state of the thread, we can get it through the getState() method. At the same time, we need to note that any thread can only be in one state at any time.

Friends who need more Java knowledge and interview questions can click the link below to get it for free

Link: 1103806531 Password: CSDN

New new state

First, we show the transition flow chart of the entire thread state. Below we will introduce it in detail. As shown in the figure below, we can intuitively see the transition of the six states. First of all, the top left is the NEW state, which is to create a new The state of the thread is equivalent to the process of our new Thread().

Insert picture description here

New represents the state where the thread is created but not yet started: when we create a new thread with new Thread(), if the thread does not start to run the start() method, then the thread does not start to execute the code in the run() method, then this At that time, its status is New. Once the thread calls start(), its state will change from New to Runnable and enter the green box in the figure

Runnable state

The Runable state in Java corresponds to the two states in the thread state of the operating system, namely Running and Ready. That is to say, the thread in the Runnable state in Java may be executing, or it may not be executing, waiting to be allocated CPU resources .

Therefore, if a running thread is in the Runnable state, when it runs to half of the task, the CPU executing the thread is scheduled to do other things, causing the thread to temporarily not run, and its state remains unchanged. It is still Runnable. Because it may be scheduled back at any time to continue the task.

Blocking state

The key state Runnable of the thread is recognized above, then let's take a look at the following three states. These three states can be collectively called the blocking state. They are Blocked (blocked), Waiting (waiting), Timed Waiting ( Time to wait).

Blocked state

First of all, let’s get to know the Blocked state. This is a relatively simple state. We can see from the diagram below that there is only one way to enter the Blocked state from the Runnable state, and that is when it fails to enter the synchronized code block. Obtain the corresponding monitor lock (we will introduce the monitor lock specifically later, here we know that the implementation of synchronized is based on the monitor lock),

Insert picture description here
On the right side, we can see that there is a connection line from the Blocked state to the Runnable, and there is only one case, then when the thread acquires the monitor lock, the thread will enter the Runnable state to participate in the grabbing of CPU resources

Waiting

We have read the blocking state above, then let's take a look at the Waiting state. There are three situations for entering the Waiting state, as shown in the following figure, respectively:

  • When the Object.wait() method without setting the Timeout parameter is called in the thread
  • When the thread calls the Thread.join() method without setting the Timeout parameter
  • When the thread calls the LockSupport.park() method

Insert picture description here
Regarding the LockSupport.park() method, let’s talk about it here. We know from the above that Blocked is for the synchronized monitor lock, but there are actually many other locks in Java, such as ReentrantLock. Among these locks, if the thread does not acquire The lock will directly enter the Waiting state. In fact, this is essentially the execution of the LockSupport.park() method to enter the Waiting state

The difference between Blocked and Waiting

  • Blocked is waiting for other threads to release the monitor lock
  • Waiting is waiting for a certain condition, such as the completion of the join thread, or notify()/notifyAll().

Timed Waiting

Finally, let’s talk about this Timed Waiting state. It is very similar to the Waiting state. The difference lies in whether there is a time limit. In the Timed Waiting state, it will wait for a timeout and then be awakened by the system, or it can be notified in advance. notify

Insert picture description here
From the above figure, we can see that the thread will enter the Timed Waiting state in the following situations.

The thread executes the Thread.sleep(long millis) method
with the time parameter ; the thread executes the Object.wait(long timeout) method
with the time parameter ; the thread executes the Thread.join(long millis) method with the time parameter ; The
thread executes the LockSupport.parkNanos(long nanos) method and LockSupport.parkUntil(long deadline) method with the time parameter set.

Through this we can further see that it is the same as the waiting state

Transition between thread states

Above we talked about the characteristics of each state and the status of the running state entering the corresponding state, then we will analyze the transition between the respective states in the future, in fact, it is mainly the transition of the three states of Blocked, waiting, and Timed Waiting, and how they enter The next state finally enters Runnable

Blocked 进入 Runnable

To enter the Runnable state from the Blocked state, we said above that the thread must obtain the monitor lock, but if you want to enter other states, it is relatively special because it does not have a timeout mechanism, that is, it will not actively enter.

As shown in the figure below, the bold purple indicates the line:

Insert picture description here
Waiting 进入 Runnable

Only when LockSupport.unpark() is executed, or the thread of the join ends, or is interrupted, can it enter the Runnable state.
The following icon note

Insert picture description here
If you call notify() or notifyAll() from other threads to wake it up, it will directly enter the Blocked state. You may have questions here. Shouldn't it go directly to Runnable? There is one point to note here, because if the thread that wakes up the Waiting thread calls notify() or notifyAll(), it must first hold the monitor lock, which is what we say wait() and notify must be in the synchronized code block.

Therefore, when the thread in the Waiting state is awakened, it cannot get the lock, and it will enter the Blocked state. Until the thread that wakes it up after executing notify()/notifyAll() is executed and the monitor lock is released, it may be its turn to grab This lock, if it can be seized, will return from the Blocked state to the Runnable state.

Insert picture description here
Here everyone must pay attention to this point. When we wake up through notify, we enter the blocking state first, and then enter the Runnable state when the monitor locks the throat!

Timed Waiting进入Runnable

The same is true for executing notify() and notifyAll() in Timed Waiting. They will enter the Blocked state first, and then return to the Runnable state after successfully grabbing the lock.

Insert picture description here
But for Timed Waiting, it has a timeout mechanism, which means that if the timeout period expires, the system will automatically get the lock directly, or when the join thread execution ends/LockSupport.unpark() is called/is interrupted, etc. Enter the Runnable state directly without going through the Blocked state

Insert picture description here

Terminated

Finally, let’s talk about the last state, the Terminated state. There are two possibilities to enter this state.

  • The run() method is executed and the thread exits normally.
  • An uncaught exception occurred, terminating the run() method, which eventually led to an unexpected termination.

to sum up

Finally, let's talk about it and pay attention to the process of thread conversion:

  • The state of the thread follows the direction of the arrow. For example, a thread cannot directly enter the Blocked state from the New state. It needs to go through the Runnable state first.
  • The thread life cycle is irreversible: once it enters the Runnable state, it cannot return to the New state; once it is terminated, there can be no state changes.
  • Therefore, a thread can only have the New and Terminated state once, and only in the intermediate state can it switch to each other. That is, these two states will not participate in mutual transformation.

Heavy benefits

I have compiled various knowledge points, modules, documents and more real interview questions from major companies. Friends in need can click the link below to get them for free

Link: 1103806531 Password: CSDN

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48655626/article/details/109201441