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

Thread state & the difference between Wait and sleep?

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

Share

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

Thread stack content parsing:

The difference between Wait and sleep?

What they have in common: they all block, wait or sleep the current thread.

Differences:

Wait (): when a thread executes on the wait () method, the current thread releases the monitor lock, and other threads can occupy the lock. Once the execution of the wait () method is completed, the current thread continues to hold the lock until the scope of the lock is executed, as shown below:

Sleep (): sleep () has nothing to do with the lock operation, and if the method happens to be within the protection of a lock, the current thread maintains the monitor lock even when the sleep is executed. As shown below:

When a thread holds a lock, the thread stack prints a-locked

When a thread is waiting for another thread to release the lock, the thread stack prints a-waiting to lock

When a thread holds a lock but executes on the lock's wait, the thread stack first prints the locked and then prints-waiting on

The state of the thread

The RUNNABLE thread is running. (threads in Runnable state do not necessarily consume CPU)

TIMED_WAITING (on object monitor) indicates that the current thread has been suspended for a period of time, indicating that the thread is executing the obj.wait (ing time) method, and that the thread does not consume cpu.

TIMED_WAITING (sleeping) indicates that the current thread is suspended for a period of time and is executing the Thread.sleep (int time) method, such as:

WAITING (on object monitor) the current thread is suspended and is executing the parameterless obj.wait () method, which can only be woken up by notify, so it does not consume cpu

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