P706) must surround Wait () with a while loop that checks for interest. The essence is to check the specific conditions of interest and return to wait () if the condition is not satisfied.
P707)notify () and Notifyall ()
with notify (), only one of the many tasks waiting for the same lock is awakened, so if you want to use Notify (), you must ensure that the appropriate task is awakened. In addition, in order to use Notify (), All tasks must wait for the same conditions , so if you have multiple tasks waiting for different conditions, then you do not know whether the appropriate task will be awakened. If you use Notify (), you must have only one task to benefit from when the condition changes . Finally, these restrictions must always work for all subclasses that may exist. If any of these rules are not satisfied, then you must use Notifyall () instead of notify ().
Notifyall () will wake up "all waiting Tasks". Does this mean that any task in the wait () state will be awakened by any call to Notifyall () anywhere in the program? In fact, when Notifyall () is called for a particular lock, only the task that waits for the lock is awakened.
P707)Timer
1 New Timer (); 2 Timer.scheduleatfixedrate (new timertask () {3 @Override4 Public void run () {5 //... 6 }7 }, (+); 8 Timer.cancel ();
P711) using an explicit lock and condition object
The basic class that uses mutexes and allows tasks to hang is condition, and you can suspend a task by raising an await () on condition. When the external condition changes, meaning that a task should continue to execute, you can call signal () to notify the task, which wakes up a task, or calls Signalall () To awaken all tasks that are suspended on this condition (as opposed to using Notifyall (), Signalall () is a safer way).
P713) producers-consumers and queues
linkedblockingqueue--unbounded Queue
arrayblockingqueue--determined, fixed size
synchronousqueue--Size fixed to 1
Java Concurrency 4