Inter-thread collaboration: Wait, notify, Notifyall

Source: Internet
Author: User

reproduced: http://blog.csdn.net/ns_code/article/details/17225469

in Java, you can use the Wait () method and the Notify () method or the Notifyall () method to invoke the object objects to enable communication between threads. Calling the Wait () method in the thread will block notifications waiting for other threads (other threads call the Notify () method or the Notifyall () method), call the Notify () method or the Notifyall () method in the thread, and notify other threads to return from the Wait () method.

Object is a superclass of all classes, and it has 5 methods that make up the core of the wait/notification mechanism: Notify (), Notifyall (), wait (), wait (long), and wait (Long,int). In Java, all classes inherit from object, so all classes have these common methods available for use. Furthermore, since they are all declared final, you cannot overwrite any one method in a subclass.

Here is a detailed description of the various methods in use of the points to note:

1. Wait ()

Public final void Wait () throws Interruptedexception,illegalmonitorstateexception

This method is used to place the current thread in hibernation until it is notified or interrupted. Before calling Wait (), the thread must obtain an object-level lock on the object, that is, the wait () method can only be called in a synchronous method or in a synchronous block. After entering the wait () method, the current thread releases the lock. Before returning from Wait (), the thread competes with the other threads to regain the lock. If you call Wait () and do not hold the appropriate lock, throw illegalmonitorstateexception, which is a subclass of RuntimeException, so you do not need the try-catch structure.

Note: The wait method frees the locks that are held by the Wait method object

  

synchronized (proceedlock) {              synchronized (this) {                print (' in Waittoproceed () '-About To wait () ");                 // release is Proceedlock this lock, this object's lock will not be released                Print ("in Waittoproceed ()-Back from Wait ()");              }        }  

2, Notify ()

Public final native void Notify () throws Illegalmonitorstateexception

The method is also called in a synchronous method or synchronous block, that is, before the call, the thread must also obtain an object-level lock on the object, and if the call to notify () does not hold the appropriate lock, the illegalmonitorstateexception is thrown.

This method is used to notify other threads that may be waiting for an object lock on the object. If there is more than one thread waiting, the thread planner picks up a thread in one of the wait () states and makes it wait for an object lock on the object (notify, the current thread does not immediately release the object lock, and the thread that waits is not immediately available for the object lock. To wait until the program exits the synchronized code block, the current thread will release the lock, and wait on the thread to acquire the object lock, but not the other threads that are waiting to be notify by the object. When the first wait thread that obtains the object lock finishes running, it frees the object lock, and if the object does not use the Notify statement again, even if the object is idle, the other wait-waiting threads continue to block in the wait state because they are not notified of the object. Until the object emits a notify or notifyall. Note here: They wait for the notify or notifyall, not the lock. This is not the case when the following Notifyall () method is executed.

3, Notifyall ()

Public final native void Notifyall () throws Illegalmonitorstateexception

This method works the same way as the Notify () method, and the important difference is:

Notifyall causes all the threads that wait on the object to exit the wait state (that is, all wake-up, no longer wait for notify or notifyall, but because the object lock has not been acquired at this time, and therefore cannot continue execution), to wait for the lock on the object. Once the object lock is freed (the notifyall thread exits calling the Notifyall Synchronized code block), they will compete. If one of the threads acquires the object lock, it will continue to execute, and after it exits the synchronized code block, releasing the lock, the other threads that have been awakened will continue to compete to acquire the lock, continuing until all the awakened threads have executed.

4. Wait (long) and wait (Long,int)

Obviously, these two methods are set to wait timeout time, the latter in the value of time with NS, precision is difficult to achieve, therefore, this method is seldom used. For the former, if you have exceeded the specified number of milliseconds before waiting for the thread to be notified or interrupted, it is competing to regain the lock and return from Wait (long). Also, you need to know that if you set a time-out, when wait () returns, we are not sure whether it was returned because of a notification or a timeout because the wait () method does not return any related information. However, it is generally possible to judge by setting the flag bit, changing the value of the flag bit before notify, reading the value of the flag bit after the Wait () method, and, of course, to ensure that the Notify is not omitted, we also need another flag bit to loop to determine whether to call the Wait () method.

Deep Understanding:

If the thread calls the object's Wait () method, the thread is in the object's wait pool, waiting for the thread in the pool to not compete for the lock on the object.

When a thread calls the object's Notifyall () method (Wake all wait threads) or the Notify () method (which only randomly wakes up a wait thread), the awakened threads go into the object's lock pool, and the thread in the lock pool competes for that object lock.

A high-priority thread competes for the probability of an object lock, and if a thread does not compete with the object lock, it will remain in the lock pool, and the Wait () method will be called again by the wired thread to return to the waiting pool. The thread that competes to the object lock continues execution until the synchronized code block is executed, releasing the object lock, and the thread in the lock pool continues to compete for that object lock.

Inter-thread collaboration: Wait, notify, Notifyall

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.