Java multithreaded design mode wait and notify mechanism summary

Source: Internet
Author: User

Java multithreaded design mode wait and notify mechanism summary:

    • The wait and notify methods must be written in the synchronized method, that is, before calling the wait and notify methods, you need to obtain the object lock;
    • call the Wait method to release the lock; After the wait method returns, you need to obtain the object lock to continue executing the following statement;
    • When multiple threads wait, if another thread calls the Notify method, the JVM decides to wake up one of the threads;
    • When multiple threads wait, if another thread calls the Notifyall method, all the wait threads are woken up, but only one of the threads can get the object lock, execute the wait statement, and wait for the object lock to be released before the rest is executable;
    • When calling Notify/notifyall, the thread does not release the object lock, except that the rest of the threads are woken up, but still cannot execute, and only one of the remaining threads can get lock execution after the call is finished and the synchronized block is released and the object lock is freed.

another:The Sleep method suspends the current thread for a period of time, allowing other threads to continue executing, but it does not release the object lock. That is, if there are synchronized blocks, other threads still cannot access the shared data.

For the producer-consumer example: The warehouse provides two synchronization methods, a put, a get, assuming that the put method thread sleep30 seconds, during this time, the Get method does not get the product from the warehouse, because sleep does not release the object lock, So the thread calling the Get method cannot execute.

Assuming that there are two threads executing at the same time, there is no call to the Synchronized method, one thread priority is max_priority, the other is min_priority, and if there is no sleep () method, a low-priority thread can execute only after the high-priority thread finishes executing But when the high-priority thread sleep (5000), the low priority has a chance to execute.

That is, the sleep method allows a low-priority thread to be executed and, of course, allows the same priority, high-priority thread to get the chance to execute.

Java multithreaded design mode wait and notify mechanism summary

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.