Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How many states are there in the java thread

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how many states are there in java threads". In daily operation, I believe many people have doubts about how many states java threads have. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "how many states are there in java threads?" Next, please follow the editor to study!

What are the six states of a thread?

People have birth, aging, sickness and death. Similarly, threads have their own life cycle. There are six states in the lifecycle of a thread in Java:

New (newly created)

Runnable (runnable)

Blocked (blocked)

Waiting (waiting)

Timed Waiting (timing and waiting)

Terminated (terminated)

When you look at the source code of the Thread class, such an enumeration class is also defined internally. This enumeration class defines the state of the thread. The source code is as follows:

Public enum State {NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED;}

PS: a thread can only be in one of the above six states at any time, and we can call getState () to check the state of the thread.

How do threads switch states?

We know that threads have six states. But how does it switch? Brother Dog made a picture according to his understanding, and then he will learn more about the switching of thread state according to this picture.

Six states of a thread

NEW (newly created)

Notice the NEW state first: the thread is in this state when it is NEW, but before the start method is called. Once the start method is called, it enters the RUNNABLE state.

RUNNABLE (runnable)

Threads in RUNNABLE are special. It also has two states: Running and Ready. That is, threads in the Runnable state in the Java may or may not be executing and are waiting for CPU resources to be allocated.

Therefore, we can infer that a thread in the Runnable state is temporarily not running when it is halfway through the task and the CPU that executes the thread is scheduled to do something else. However, its state remains the same, still Runnable, because it may be scheduled back at any time to continue to execute the task.

That is: a thread in the Runnable state is not necessarily running. This point is often asked in the interview, friends should pay attention.

Blocked (blocked)

Looking back at the simplest Blocked state, there is only one possibility of entering Blocked from Runnable: enter the code protected by the synchronized keyword, but do not get the monitor lock.

Six states of a thread

Next, on the right side of the figure, Blocked-> Runnable status: when the thread in the Blocked state acquires the lock.

Waiting (waiting)

There are three possibilities for a thread to enter the Waiting state from Runnable:

There is no Object.wait () method that sets the Timeout parameter.

There is no Thread.join () method that sets the Timeout parameter.

The LockSupport.park () method.

As we know above, the only way for a thread to enter the Blocked state is to enter the synchronized-protected code, but not to get the monitor lock. However, there are many locks in Java, such as ReentrantLock. When a thread acquires such a lock, it enters the Waiting state without grabbing it, because it essentially calls the LockSupport.park () method.

Similarly, calls to Object.wait () and Thread.join () also put the thread into a waiting state.

The difference between Blocked and Waiting is that Blocked is waiting for other threads to release the monitor lock, while Waiting is waiting for a condition, such as join's thread to finish execution, or notify () / notifyAll ().

Six states of a thread

Look at the right side of Waiting, Waiting-> Runnable:1, when LockSupport.unpark () is executed, 2. The thread of join ends, 3.

Look at the right side of Waiting, Waiting-> Blocked: if we look at the figure, we can see that other threads call notify () or notifyAll () to wake up the thread in Waiting, which goes directly into the Blocked state. Why is that?

Other threads can call notify () or notifyAll () to try to wake up the Waiting state thread, indicating that it must hold the same monitor lock, that is, the thread in Waiting cannot immediately grab the monitor lock after it is awakened, so it must first enter the Blocked state. After the thread that awakens it releases the lock, it can grab the lock and enter the Runnable state from Blocked.

Timed Waiting (timing and waiting)

The difference between this state and the Waiting state is that there is no time limit, the Timed Waiting will wait for the timeout, wake up automatically by the system, or be woken up by the wake-up signal before the timeout.

There are five situations that can put a thread into a Timed Waiting state:

Set the Thread.sleep (time) method of the Timeout parameter.

Set the Object.wait (time) method of the Timeout parameter.

Set the Thread.join (time) method of the Timeout parameter.

Set the LockSupport.parkNanos (long nanos) method of the Timeout parameter.

Set the LockSupport.parkUntil (long deadline) method of the Timeout parameter.

Six states of a thread

To the right of Timed Waiting, Timed Waiting-> Blocked: like Waiting, other threads execute notify () and notifyAll (). The current thread also enters the Blocked state first, and then decides whether to enter the Runnable state depending on the acquisition of the lock.

In addition, Timed Waiting-> Runnable: 1, the timeout of the current thread is up and the lock can be obtained directly, 2, the thread of join ends, 3, it is interrupted, 4, LockSupport.unpark () is called, these cases will be directly restored to Runnable without going through the Blocked state.

Terminated (terminated)

Finally, it is relatively easy to enter the termination state. There are three situations:

When the task is finished, the thread exits normally.

An uncaught exception occurs (such as a direct call to the interrupt () method).

At this point, the study of "how many states are there in java threads" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report