Why Wait (), notify (), and Notifyall () must be adjusted in the synchronization block or synchronous method

Source: Internet
Author: User

We often use the wait (), notify (), and Notifyall () methods to communicate between threads. When a thread checks a condition, it enters a wait state, for example, in the producer-consumer model, the producer thread discovers that the buffer is full and waits, and the consumer thread makes the buffer idle and notifies the producer thread by consuming a product. The Call of Notify () or Notifyall () notifies one or more threads that the condition has changed, and that once the notification thread leaves the synchronization block, all threads waiting for this object lock will compete for this object lock, and the lucky thread obtains the lock from Wait () method returns and continues execution. Let's take a few steps to look at the race condition between the wait () and Notify () methods (race condition), and we'll use the "producer-consumer" model to make it easier to understand the scenario:
    1. Producer Thread test condition (the buffer is full) and determines that it must wait (when the buffer is full)
    2. Consumer threads set conditions after consuming a product from a buffer
    3. The consumer thread calls the Notify () method, and the message is ignored because the producer thread is not waiting at this time.
    4. The producer thread calls the Wait () method and enters the wait state.
So, because of the competitive conditions here, we may lose a notification, if we use buffers or only one product, the producer thread will wait forever and your program will hang up.
Now let's consider how this potential competitive condition can be solved. This competition condition can be resolved by using the synchronized keywords and locks provided by Java. In order to invoke the Wait (), notify (), and Notifyall () methods, we must obtain a lock on the object that calls these methods. Because the wait () method frees the lock before it waits and then re-obtains the lock before the Wait () method returns, we must use this lock to ensure that the check condition (the buffer is full) and the set condition (from the buffer fetch the product) are atomic, which can be implemented by synchronous blocks or synchronous methods.
In short, we call the Wait (), notify (), and Notifyall () methods from synchronous blocks or synchronous methods to avoid:
    • Illegalmonitorstateexception, the system throws this exception if we do not call these methods through the synchronization environment (synchronized context)
    • Any potential competition conditions between wait () and notify ().

Description: Omitted some unimportant passages from the original text.

Original address: http://javarevisited.blogspot.com/2011/05/wait-notify-and-notifyall-in-java.html



Why Wait (), notify (), and Notifyall () must be adjusted in the synchronization block or synchronous method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.