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 do java sleep and wait understand

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

Share

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

This article mainly explains "how to understand java sleep and wait". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand java sleep and wait".

Sleep lets the thread end from [running]-> [blocking state] time / interrupt-> [runnable]

Wait lets threads from [running]-> [waiting queue] notify-> [lock pool]-> [runnable]

First, remember the difference: "sleep is a method of the Thread class, and wait is a method defined in the Object class." Although both methods affect the execution behavior of threads, they are essentially different.

Thread.sleep does not cause a change in lock behavior, and Thread.sleep does not let the thread release the lock if the current thread owns the lock. If it helps you remember, you can simply assume that the lock-related methods are defined in the Object class, so calling Thread.sleep does not affect the lock-related behavior.

Both Thread.sleep and Object.wait pause the current thread, and for CPU resources, no matter how the thread is paused, it no longer needs CPU execution time for the time being. OS allocates execution time to other threads. The difference is that after calling wait, you need another thread to execute notify/notifyAll to regain the CPU execution time.

The state of the thread refers to the definition of Thread.State. The newly created thread that has not been executed (which has not yet called start ()) is in the "ready", or Thread.State.NEW state.

Thread.State.BLOCKED means that when a thread is acquiring a lock, it is forced to suspend execution of the following instruction because the lock cannot be acquired until the lock is released by another thread. For a thread in BLOCKED state, the OS scheduling mechanism needs to decide which thread can acquire the lock next. In this case, it is a lock contention, which is a time-consuming operation anyway.

Thank you for your reading, the above is the content of "how to understand java sleep and wait". After the study of this article, I believe you have a deeper understanding of how to understand java sleep and wait, 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