Java multithreaded Learning (iv)--Thread interaction

Source: Internet
Author: User

Three basic functions used in thread interaction:

void Notify (); Wakes a single thread waiting on this object monitor.

void Notifyall (); Wakes all the threads waiting on this object monitor.

void Wait () causes the current thread to wait until another thread calls the Notify () or Notifyall () method of this object.

The overloaded version of the Wait () also causes the current thread to wait until another thread calls the Notify () or Notifyall () method of this object, or waits longer than the specified time.

void Wait (long timeout,int Nanos); the overloaded version of Wait () also causes the current thread to wait, until another thread calls this object's notify () or Notifyall () method, or waits longer than an actual time, or another thread interrupts the current thread.

In the thread interaction, it is important to note that:

The Wait (), notify (), Notifyall () methods must be called from the synchronization environment, and the thread cannot invoke methods on the object to wait or notify unless the thread owns the lock on the object.

The thread obtains a waiting list by executing the Wait () method on the object, and then the thread no longer executes any other instructions until the object's notify () method is called. When multiple threads are waiting on the same object, notify () will randomly wake up one of the threads to make it run. If no threads are waiting, no processing is done.

The wait () method is executed as long as the current thread owns the lock on the object, and after the Wait () method is executed, the thread frees the lock on the object so that it can be called notify () or Notifyall () in other threads to wake the thread. However, when calling notify (), it does not mean that the thread will release the lock on the object. If the thread is still completing the synchronization code, the thread does not release the lock on the object when it finishes synchronizing the code block. Therefore, notify () does not mean that the lock becomes available.


Note the issue:

Wait (), notify (), Notifyall () are not part of the thread class, but belong to the object class, which means that each object has three methods. Because each object has its own lock, the lock is the basis for each object, and the method of manipulating the lock is also the basis for each object.

Wait () causes the current thread to wait instead of waiting for the thread class where the object is called, that is, which thread owns the lock on the object to invoke the Wait () method.

Notify () wakes up a single thread waiting on the current object monitor, and if there are multiple threads waiting on this object, a random wake-up period is a thread until the current thread discards the lock on this object before it can continue to execute the wake-up thread. Notify (), like wait (), must be called by the thread holding the lock on the object, Notifyall (), but Notifyall () wakes up all the threads waiting on the object and randomly selects one from the thread.

All three methods need to be used in conjunction with synchronized, that is, the lock of the object must be obtained before the caller three methods.


Java multithreaded Learning (iv)--Thread interaction

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.