Java Concurrency Programming (11) Omission of communication notify notifications between threads

Source: Internet
Author: User

The omission of notify notice is easy to understand, that is, Threada has not started wait time, THREADB has notify, so, THREADB notice is not any response, when threadb exit synchronized code block, Threada starts waiting again and then waits until it is interrupted by another thread.

See Example: Https://git.oschina.net/wenjieyatou/threadTest

Before THREADB notifies, the Oktoproceed is set to true so that if Threada misses the notification, it will not enter the while loop, and the wait method will not be executed, and the thread will not block. If the notification is not omitted, after the wait method returns, the Oktoproceed has been set to true, and the next while loop judgment condition does not hold, it exits the loop.

In this way, the use of the sign and wait, notifyall to avoid the omission of the notice caused by blocking problems.

Summary: When using the thread's wait/notification mechanism, it is generally necessary to match a Boolean value (or any other condition that can determine the true or false) before notify changes the value of the Boolean variable. Having wait return can exit while loop (typically with a while loop on the periphery of the wait method to prevent early notification), or after the notification has been omitted, it will not be blocked at the wait method. This guarantees the correctness of the program.

Java Concurrency Programming (11) Omission of communication notify notifications between threads

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.