Wait (), notify () and Notifyall (), Sleep (), Condition, await (), signal ()

Source: Internet
Author: User
Tags thread class

Wait (), notify (), and Notifyall () are methods in the object class
From the textual description of these three methods, you can know the following information:
1 the Wait (), notify (), and Notifyall () methods are local methods and cannot be overridden for the final method.
2 The Wait () method that invokes an object can block the current thread and the current thread must own the monitor (that is, the lock) of the object.
3 The Notify () method that invokes an object wakes up a thread that is waiting for the monitor for the object, and wakes only one of the threads if multiple threads are waiting for the object's monitor;
4 Call the Notifyall () method to wake up all the threads that are waiting for monitor on this object;
5 if the Wait () method of an object is invoked, the current thread must own the monitor (that is, the lock) of the object, so invoking the wait () method must be done in either the synchronized block or the synchronization method (synchronized block or Synchronized method).
6 Call an object's Wait () method, which is equivalent to having the current thread hand over the monitor for this object and then into the waiting state, waiting for the subsequent lock of this object again (the Sleep method in the thread class suspends the current thread for a period of time so that other threads have the chance to continue executing, But it does not release object locks);
7 The Notify () method of an object is invoked, and the current thread must own the monitor of the object, so the call to the Notify () method must be performed in either the synchronized block or the synchronization method (synchronized block or Synchronized method).

Condition is present in Java 1.5, which replaces the traditional wait (), notify () of the object to implement the collaboration between threads, compared to the Wait (), notify () using object, await (), Signal () This approach enables more secure and efficient threading collaboration.
Therefore, it is generally recommended to use condition.

1 condition is an interface, the basic method is await () and signal () method;
2 condition relies on the lock interface, the basic code to generate a condition is lock.newcondition ()
3 the await () and signal () methods of calling condition must be within lock protection, that is, they must be used between Lock.lock () and Lock.unlock.

The await () in the Conditon corresponds to the Wait () of object;
The signal () in the condition corresponds to the Notify () of the object;
Signalall () in condition () corresponds to the Notifyall of object ()

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.