In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what is the use of wait, notify and notifyAll". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "what is the use of wait, notify and notifyAll?"
One: a brief introduction to wait, notify and notifyAll
1. Why wait, notify, notifyAll are not methods of thread object Thread
Reason: first of all, you should make it clear that the wait, notify, and notifyAll methods are all operations on the lock object, and the lock can be any object. In the world of java, any object belongs to the Object class, so all three methods are methods of Object, not thread object Thread.
two。 There are two points to pay attention to when using the method:
a. These three methods must be used within the critical section (simply understood, code block) contained in the synchronized keyword
b. The mode of use is the lock object. Method (), such as obj.wait ()
3. When to add the lock and when to release the lock? The following points must be made clear:
A. the notify and notifyAll methods do not release the lock, but these two methods simply inform other threads that use the lock but are in the wait state to prepare for the lock (note: it is also important to note here that other threads that use the lock as the lock and are in the wait state are eligible to seize the lock only if they are awakened by notify or notifyAll)
b. When a lock object calls the wait method, the object lock of the current thread is immediately released, and other threads pass through the
When the notify/notifyAll method tells the thread that it can seize the object lock, if the current thread does, it will start execution after the wait method of the current lock-that is, where the wait is and where it is executed.
c. There is only one way to add a lock in the combination of synchronized, wait, notify and notifyAll, that is, when you enter the synchronization code block
There are two ways to release the lock: the ① lock object releases the lock when it calls the wait method, and the ② automatically releases the lock when it finishes synchronizing the block of code.
4. The difference between notify and notifyAll
a. The notify of a lock object will only wake up a thread that uses the lock as a lock and is in the wait state
b. The notifyAll method of a lock object wakes up all threads that use the lock as a lock and are in the wait state
Usage suggestion: to prevent some threads from being notified, it is recommended to use notifyAll.
The above is all the content of this article "what is the use of wait, notify and notifyAll?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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.