Java multi-thread design mode: Wait/notify mechanism)

Source: Internet
Author: User
Usually, coordination is required between multiple threads. For example, if displaythread of a browser that displays an image wants to execute the task of displaying the image, it must wait for the download thread downloadthread to download the image. If the image has not been downloaded, displaythread can be paused. After downloadthread completes the task, it notifies displaythread that "the image is ready and displayed". At this time, displaythread continues to run.

The above logic simply says: if the conditions are not met, wait. When the condition is met, the thread waiting for the condition will be awakened. In Java, the implementation of this mechanism depends on wait/notify. The wait and lock mechanisms are closely related. For example:

Synchronized (OBJ ){
While (! Condition ){
OBJ. Wait ();
}
OBJ. dosomething ();
}

After thread a acquires the OBJ lock, it finds that the condition is not met and cannot continue the next processing. Therefore, thread a is wait ().

In another thread B, if B modifies some conditions so that the condition of thread a is met, thread A can be awakened:

Synchronized (OBJ ){
Condition = true;
OBJ. Y ();
}

Note the following concepts:

# Obtain the OBJ lock before calling the wait () and notify () Methods of OBJ, that is, the OBJ lock must be written in synchronized (OBJ ){...} Code .

# Call obj. after wait (), thread a releases the OBJ lock. Otherwise, thread B cannot obtain the OBJ lock, and it cannot be in synchronized (OBJ ){...} wake up a in the code segment.

# After the obj. Wait () method is returned, thread a needs to obtain the OBJ lock again to continue execution.

# If both A1, A2, and A3 are in OBJ. wait (), B calls obj. notify () can only wake up one of A1, A2, and A3 (which one is determined by JVM ).

# Obj. notifyall () can all wake up A1, A2, A3, but continue to execute obj. the next statement of wait () must obtain the OBJ lock. Therefore, only one of A1, A2, and A3 has the opportunity to obtain the lock for further execution. For example, A1, the rest can be executed only after A1 releases the OBJ lock.

# When B calls obj. Y/policyall, B is holding the OBJ lock. Therefore, although A1, A2, and A3 are awakened, they still cannot obtain the OBJ lock. After B exits the synchronized block and releases the OBJ lock, only one of A1, A2, and A3 will have the opportunity to obtain the lock and continue execution.

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.