Introduction: Yesterday try to use Java to implement the producer consumer problem (Producer-consumer problem), in coding, the use of condition await and signalall method, and then by the way remembered wait and notify , a problem has been encountered in development: what are the timing for wait, notify, and other blocking and recovery? Google has been on the internet for a long time after the various blog posts, found that few people mentioned this point. Finally, we found the corresponding introduction in the official documents.
(i) Preparation
It should be customary to introduce the basics of wait, notify, and Notifyall first. I found a good article: Java's Wait (), notify (), and Notifyall () usage summary, which even describes why wait and other methods must first obtain an object lock. I'm not going to repeat that here.
(ii) Congestion and recovery
(1) Wait method
The wait method inherits from the object class (the method modifier is fianl native, which also explains why the wait method cannot be overridden in the condition Class), with a total of three methods:
public final void Wait (long timeout) throws Interruptedexception public final void Wait (long timeout, int Nanos) throws interruptedexception public final void wait () throws interruptedexception
blocking: calls to these three methods will cause the current thread to block. The thread will be placed in the request waiting queue for the object, and then all the synchronization requests that are currently owned by object are conceded. The thread suspends all thread scheduling until one of the following conditions occurs:
① the other thread calls the Notify method of the object, which is exactly the thread that was awakened;
② Other threads call the Notifyall method of the object;
③ other objects interrupt/kill the thread;
④ (in this case, only for the first two methods) the thread waits for the specified time;
recovery: The thread will be removed from the wait queue and re-become a scheduled thread. It competes with other threads in the normal way for object synchronization requests. Once it re-obtains the synchronization request for the object, all previous request states are restored, that is, the state of the thread where wait is called. The thread will continue to run where wait was previously called.
(2) Notify and Notifyall methods
The Notify function is to wake up a thread in the request queue, and Notifyall wakes up all the threads in the request queue.
A thread that is awakened does not run immediately unless the lock of the object is acquired. That is, the thread that calls notify, after calling notify, does not immediately block the running of the thread, like wait. Instead, continue running until the corresponding thread is dispatched or the lock of object is conceded. the awakened thread competes for the object lock in the usual manner with other threads, as mentioned above, after the current thread has conceded an object lock.
Resources:
Https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html
Java Multithreaded Learning notes: Wait, notify, notifyall blocking and recovery