Java Concurrency Programming (10) using Wait/notify/notifyall for inter-thread communication

Source: Internet
Author: User
Tags instance method

Reprint Please specify the Source: http://blog.csdn.net/ns_code/article/details/17225469

Wait () Method: Publicfinal void Wait () throws Interruptedexception,illegalmonitorstateexception

  The wait () method causes the current thread to wait until another thread calls the Notify () or Notifyall () method.

  The current thread must have the monitor for the current object, or lock, which is the lock.

The thread calls the Wait () method, frees its ownership of the lock, and waits for another thread to notify it (the notification is in the Notify () or Notifyall () method) so it can regain ownership of the lock and resume execution.

To ensure that you have a lock when you call the Wait () method, the call to the wait () method must be placed in the synchronized method or synchronized block.

  A small comparison:

When a thread calls the Wait () method, it releases the lock on the object.

Another method that causes the thread to pause: Thread.Sleep (), which causes the thread to sleep for a specified number of milliseconds, but the thread does not release the lock on the object during sleep.

Notify () method public final native void Notify () throws Illegalmonitorstateexception

  The Notify () method wakes up a thread that waits for a lock on the current object.

If multiple threads are waiting, one of them will choose to be awakened. This choice is arbitrary and relevant to the specific implementation. (a thread waits for a lock on an object because it calls one of the wait methods.)

  A thread that is awakened cannot be executed and needs to wait until the current thread discards the lock on the object.

The awakened thread will compete with other threads in the usual way to get the lock on the object. In other words, the awakened thread does not have any priority or disadvantage, and the next thread of the object needs to compete in general.

  The Notify () method should be called by the thread that owns the lock for the object.

(This method should only being called by a thread that's the owner of this object ' s monitor.)

In other words, like the Wait () method, the Notify method call must be placed in the synchronized method or synchronized block.

  The Wait () and notify () methods require that the thread has acquired the lock on the object at the time of invocation, so calls to both methods need to be placed in the synchronized method or synchronized block.

  The owner of a lock that becomes a thread of an object is done in the following three ways:

1. Execute the Synchronized instance method for this object.

2. Execute the SYNCHRONIZED statement block for this object. This object is locked by this block of statements.

3. For the Class class object, execute the synchronized, static method of that category.

Notifyall ()

Public final native void Notifyall () throws Illegalmonitorstateexception

This method works the same way as the Notify () method, and the important difference is:

Notifyall All the threads that originally waited on the object to exit the wait state (that is, all wakes up, no longer waits for notify or notifyall, but because the object lock has not been acquired at this time, and therefore cannot continue down), becomes waiting to get the lock on the object, and once the object lock is freed (the notifyall thread exits the call to Notifyall's synchronized code block), they compete. If one of the threads acquires the object lock, it will continue to execute, and after it exits the synchronized code block, releasing the lock, the other threads that have been awakened will continue to compete to acquire the lock, continuing until all the awakened threads have executed.

Deep Understanding:

If the thread calls the object's Wait () method, the thread is in the object's wait pool, waiting for the thread in the pool to not compete for the lock on the object.

When a thread calls the object's Notifyall () method (Wake all wait threads) or the Notify () method (which only randomly wakes up a wait thread), the awakened threads go into the object's lock pool, and the thread in the lock pool competes for that object lock.

A high-priority thread competes for the probability of an object lock, and if a thread does not compete with the object lock, it will remain in the lock pool, and the Wait () method will be called again by the wired thread to return to the waiting pool. The thread that competes to the object lock continues execution until the synchronized code block is executed, releasing the object lock, and the thread in the lock pool continues to compete for that object lock.

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

Java Concurrency Programming (10) using Wait/notify/notifyall for inter-thread communication

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.