Wait, notify, Notifyall.

Source: Internet
Author: User

Wait (), notify (), and Notifyall () are methods in the object class:

12345678910111213141516171819202122232425 /** * Wakes up a single thread that is waiting on this object‘s * monitor. If any threads are waiting on this object, one of them * is chosen to be awakened. The choice is arbitrary and occurs at * the discretion of the implementation. A thread waits on an object‘s * monitor by calling one of the wait methods */publicfinalnativevoidnotify();/** * Wakes up all threads that are waiting on this object‘s monitor. A * thread waits on an object‘s monitor by calling one of the * wait methods. */publicfinalnativevoidnotifyAll();/** * Causes the current thread to wait until either another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object, or a * specified amount of time has elapsed. * <p> * The current thread must own this object‘s monitor. */publicfinalnativevoid wait(longtimeout) throwsInterruptedException;

The following information can be found from the text descriptions of these three methods:

1) The Wait (), notify (), and Notifyall () methods are local methods and are final methods and cannot be overridden.

2) The Wait () method that invokes an object can cause the current thread to block, and the current thread must have a monitor (that is, a lock) on this object

3) Invoking an object's Notify () method wakes up a thread waiting for the object's monitor, and if more than one thread waits for the object's monitor, only one of the threads can be awakened;

4) Call the Notifyall () method to wake up all the threads that are waiting for this object's monitor;

A friend may have questions about why these three are not methods in the thread class declaration, but rather methods declared in the object class (since the thread class inherits the object class, so thread can also call three methods)? In fact, the problem is simple, because each object has a monitor (that is, lock), so the current thread to wait for an object's lock, of course, this object should be manipulated. Instead of using the current thread, the current thread may be waiting for locks on multiple threads, which can be very complex if manipulated by a thread.

As mentioned above, if you call the Wait () method of an object, the current thread must have the monitor (that is, the lock) of the object, so calling the wait () method must be done either in a synchronous block or in a synchronous method (synchronized block or Synchronized method).

Calling the Wait () method of an object is equivalent to having the current thread hand over the object's monitor, then going into a wait state, waiting for a subsequent lock on the object (the sleep method in the thread class causes the current thread to suspend execution for a period of time, allowing other threads to continue to execute. But it does not release the object lock);

The Notify () method wakes up a thread that is waiting for the object's monitor, and when there are multiple threads waiting for the object's monitor, only one of the threads can be woken up, and it is unclear which thread to wake.

Similarly, the Notify () method of an object is called, and the current thread must have the monitor for that object, so calling the Notify () method must be done either in a synchronous block or in a synchronous method (synchronized block or Synchronized method).

The Nofityall () method can wake all threads that are waiting for the object's monitor, which is different from the Notify () method.

One thing to note here: the Notify () and Notifyall () methods simply wake up the thread that waits for the object's monitor, and do not determine which thread can get to monitor.

As a simple example: if there are three threads Thread1, Thread2, and Thread3 waiting for the monitor of the object Objecta, THREAD4 has the monitor of the object objecta, When the Objecta.notify () method is called in Thread4, only one of Thread1, Thread2, and Thread3 can be awakened. Note that being awakened does not mean that the OBJECTA Monitor is acquired immediately. If the Objecta.notifyall () method is called in Thread4, the three threads of Thread1, Thread2, and Thread3 will be awakened. As for which thread is next able to get to objecta monitor, it depends on the operating system's schedule.

In particular, it is important to note that a thread being awakened does not represent a monitor that immediately acquires the object, but only when the Notify () or Notifyall () is called and exits the synchronized block, and the remaining threads can get lock execution after the object lock is released.

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.