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 differences between Java multithreaded sleep and wait

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

Share

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

This article will explain in detail what the difference between Java multithreaded sleep and wait is, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

We all know that sleep is to let the thread sleep, to continue execution after the time, wait is to wait, need to wake up and continue execution, then these two methods in multi-threaded performance, what is the difference between them?

It can be summarized as follows.

use

From a usage perspective, sleep is a method of the Thread class, while wait is a method of the Object top-level class.

Sleep can be used anywhere, while wait can only be used in synchronization methods or synchronization blocks.

CPU and resource lock release

sleep,wait will suspend the current thread and give up CPU execution time after calling, but the difference is that sleep will not release the lock resources currently held by the object, and will continue to execute after the time, while wait will abandon all locks and need to notify/notifyAll to regain the object lock resources before continuing to execute.

exception catching

Sleep needs to catch or throw exceptions, while wait/notify/notifyAll doesn't.

About "Java multithreaded sleep and wait what is the difference" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

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