Java multithreading (4) -- thread Interaction

Source: Internet
Author: User

Java multithreading (4) -- thread Interaction

Three basic functions used in thread Interaction:

Void y (); wake up a single thread waiting on this object monitor.

Void policyall (); wake up all threads waiting on this object monitor.

Void wait (); causes the current thread to wait until other threads call the notify () or yyall () method of this object.

Void wait (long timeout); the overloaded version of wait () also causes the current thread to wait until other threads call the notify () or yyall () method of this object, or wait until the specified time is exceeded.

Void wait (long timeout, int nanos); the overloaded version of wait () also causes the current thread to wait until other threads call the notify () or yyall () method of this object, or wait for more than a certain actual time to stop waiting, or another thread to interrupt the current thread.

Note the following in thread Interaction:

The wait (), Y (), and notifyAll () methods must be called from the synchronization environment. A thread cannot call Methods waiting for or notifying objects unless the thread has an object lock.

The thread obtains the Waiting List by executing the wait () method on the object. After that, the thread no longer executes any other commands until the notify () method of the object is called. When multiple threads wait on the same object, notify () will randomly wake up one of the threads to run them. If no thread waits, no processing is performed.

To execute the wait () method, the premise is that the current thread has the Lock of this object. After the wait () method is executed, this thread releases the Lock of this object, so that other threads can call y () or notifyAll () to wake up this thread. However, calling notify () does not mean that the thread will release the object lock. If the thread is still completing the synchronization code, the thread will not release the object lock when it completes the synchronization code block. Therefore, notify () does not mean that the lock is available.


Notes:

Wait (), Policy (), and policyall () do not belong to the Thread class, but to the Object class. That is to say, each Object has three methods. Because each object has its own lock, the lock is the basis of each object, and the operation lock method is also the basis of each object.

Wait () causes the wait of the current thread, instead of the thread class wait where the calling object is located, that is, the thread that owns the Lock of this object can call the wait () method.

Y () wakes up a single thread waiting on the current object monitor. If there are multiple threads waiting on this object, a thread is randomly woken up, the wake-up thread can continue to be executed until the current thread abandons the lock on this object. Notify () is the same as wait (), and must be called by the thread holding the Lock of this object. The same is true for yyall (), but policyall () it will wake up all threads waiting for this object and randomly select one from this thread.

All three methods must be used with synchronized, that is, the object lock must be obtained before the three methods can be called.


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.