Java Concurrency 4

Source: Internet
Author: User

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

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.