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 ()