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

What are the lifecycle states of Java threads

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the lifecycle states of Java threads". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn what the lifecycle states of Java threads are.

There are a total of six states in the life cycle of threads in Java.

New (newly created)

Runnable (runnable)

Blocked (blocked)

Waiting (waiting)

Timed Waiting (timing and waiting)

Terminated (terminated)

If you want to determine the current state of a thread, you can use the getState () method, and the thread can only be in one state at any time.

One: six states of the thread 1.1:New new

New represents the state in which a thread has been created but has not been started: when we create a new thread with new Thread (), if the thread does not start running the start () method, its state is New.

1.2:Runable is runnable

Once the thread calls start (), its state changes from New to Runnable.

1.3: blocking statu

The blocking state in Java is usually not only Blocked, but actually includes three states, namely, Blocked (blocked), Waiting (waiting) and Timed Waiting (timing waiting), which are collectively referred to as blocking states.

1.3.1:Blocked is blocked

The only way to move from the Runnable state to the Blocked state is to enter synchronized protected code without grabbing the monitor lock. When the thread in Blocked grabs the monitor lock, it returns from the Blocked state to the Runnable state.

1.3.2:waiting wait

There are three ways to get a thread into the Waiting state from Runable:

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.

The previous Blocked (blocked state) is only for synchronized monitor locks, but there are many other locks in Java, such as ReentrantLock, if the thread does not grab the lock when acquiring the lock, it will enter the Waiting state, because it essentially executes the LockSupport.park () method, so it will enter the Waiting state.

To move a thread from the Waiting state to the Runable state:

Waiting is unlimited, which means it won't recover actively no matter how long it takes. You can enter the Runnable state only when LockSupport.unpark () is executed, or when the thread of join ends, or when it is interrupted.

To move a thread from the Waiting state to the Blocked state:

When another thread calls notify () or notifyAll () to wake it up, it goes directly into the Blocked state. Because the thread that wakes up the Waiting thread calls notify () or notifyAll () and requires that it must hold the monitor lock first, the thread in the Waiting state cannot get the lock when it is awakened, it will enter the Blocked state until the thread that wakes it up with notify () / notifyAll () finishes executing and releases the monitor lock, it may be its turn to snatch the lock, and if it can, it will return to the Runnable state from the Blocked state. As shown in the following figure:

1.3.3:Timed Waiting timing waiting

Above the Waiting is the Timed Waiting state, the two states are very similar, except 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 several situations for a thread to enter the Timed Waiting state:

Thread.sleep (long millis) method with time parameter set

Thread.join (long millis) method with time parameter set

Object.wait (long timeout) method with time parameter set

LockSupport.parkNanos (long nanos) method and LockSupport.parkUntil (long deadline) method with time parameters set.

To move a thread from the Timed Waiting state to the Runable state:

If its timeout expires and (the thread that can get the lock or join directly ends running, is interrupted, or calls LockSupport.unpark ()), it returns directly to the Runnable state without having to experience the Blocked state.

To move a thread from the Timed Waiting state to the Blocked state:

Like the waiting state, the same goes for notify () and notifyAll () in Timed Waiting, which first enter the Blocked state and then return to the Runnable state after successfully grabbing the lock.

1.4:Terminated termination

Terminated terminates state, there are two possibilities to enter this state.

After the execution of the run () method, the thread exits normally.

An uncaught exception occurred, which terminated the run () method, resulting in unexpected termination

1.5 Section

The state of the thread needs to go in the direction of the arrow. For example, a thread cannot enter the Blocked state directly from the New state, it needs to go through the Runnable state first.

The thread life cycle is irreversible: once you enter the Runnable state, you cannot return to the New state; once terminated, there can be no further state changes. So a thread can only have one New and Terminated state, and only in the middle state can it be switched to each other.

Second: the correspondence between Java thread state and operating system thread state 2.1 thread state in operating system

2.2 what state does the Runable state of Java correspond to in the operating system?

The Runable state in Java corresponds to two states in the operating system thread state, Running and Ready, because threads in Runnable state in Java may or may not be executing and are waiting for CPU resources to be allocated.

So, if a running thread is in Runnable state, when it is halfway to the task, the CPU executing the thread is scheduled to do something else, resulting in the thread temporarily not running, its state remains unchanged, or Runnable, because it may be scheduled back to continue to execute the task at any time.

2.2 what state does the Waiting/Timed Waiting/Blocked state of Java correspond to in the operating system?

Whether it's Timed Waiting, Waiting, or Blocked, it all corresponds to the Waiting state of the operating system thread.

Thank you for your reading, the above is the content of "what is the life cycle state of Java threads". After the study of this article, I believe you have a deeper understanding of the life cycle state of Java threads, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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