In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article to share with you is about how to understand java thread knowledge point sleep and wait, Xiaobian feel quite practical, so share to everyone to learn, I hope you can read this article after some harvest, not much to say, follow Xiaobian to see it.
Difference between sleep and wait
In terms of the meaning of the word, both have the meaning of causing threads to pause, but the implementation principle and the effect used are completely different.
Sleep puts a thread to sleep for a certain amount of time without releasing any locks, but it may not get execution time after it has passed its sleep time! The final schedule is determined by the CPU! Thread.sleep() is a static method that can be called from any context. Thread.sleep() Suspends the current thread!
Wait () is an instance method for thread synchronization. It can be called on any object because it is defined on java.lang.Object, but only from within a synchronization block. It releases the lock on the object so that another thread can jump in and acquire the lock. Wait literally means to wait. It waits for notify or notifyAll to wake it up!
Calling wait directly throws a java.lang.IllegalMonitorStateException because the object lock has not yet been acquired and cannot be released.
Do you think wait can replace sleep?
Of course, it can't be replaced, because one of the most important features is that wait will release the lock, while sleep won't release the lock!
Difference between sleep and wait
The sleep () thread controls its own flow.
wait () is used for inter-thread communication to make the thread that owns the object lock wait until a specified time or notify ()
wait() releases locks and monitors, sleep() does not release any locks or monitors, etc.
wait() is used for inter-thread communication, while sleep() is used to introduce pauses during execution
wait can only be placed in a synchronized statement block to make sense
sleep from Thread class, and wait from Object class
sleep must catch exceptions, while wait, notify, and notifyAll do not.
sleep is a static class method of Thread, who calls who goes to sleep, even if the sleep method of b is called in thread a, actually a goes to sleep, to make thread b sleep, sleep is called in the code of b.
Thread.Sleep(0) triggers the operating system to immediately resume a CPU race.
wait method once the timeout expires, does not need other threads to execute notify can automatically unblock, but if the timeout is not set wait method must wait for other threads to execute notify
They can all be interrupted by the interrupted method. The usage scenarios can be applied in the producer/consumer model.
The above is how to understand sleep and wait in Java thread knowledge points, Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.