Inter-thread collaboration: Wait, notify, Notifyall

Source: Internet
Author: User

Inter-thread collaboration: Wait, notify, Notifyall

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 the use of a few points to note.

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.

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 causes it to wait for the object lock on the object (notify, the current thread does not immediately release the object lock, and the wait thread does not immediately get the object lock, waiting for the program to exit Synchronized the code block, the current thread releases the lock, and the thread that waits for it can acquire the object lock, but does not disturb other threads that are also 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.

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.

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.