Java thread collaboration wait (wait) and notiy (notification), waitnotiy

Source: Internet
Author: User

Java thread collaboration wait (wait) and notiy (notification), waitnotiy
1. wait (), Policy (), and policyall ()

To better support multi-thread collaboration, JDK provides three important local methods.

// Call the wait () method of an object to block the current thread and the current thread must own the Lock of this object. public final void wait () throws InterruptedException {wait (0);} // call the Y () method of an object to wake up a thread waiting for the lock of this object, if multiple threads are waiting for the lock of this object, only one of the threads can be awakened. public final native void every Y (); // policyall () the method can wake up all threads waiting for this object lock; public final native void policyall ();

When a thread with the Object lock calls the wait () method, it will add the current thread to the object. wait waits for the queue and releases the currently occupied Object lock. In this way, other threads have the opportunity to obtain the Object lock. The thread that obtains the Object lock can call the notify () method in the Object. wait waits for the queue to wake up a thread randomly (this wake-up is random and irrelevant to the order of addition, and the probability of being awakened with a higher priority will be high). If policyall () is called () method To wake up all threads. Note: After the notify () method is called, the object lock will not be released immediately and will be released after the thread completes execution.

Code:

Public class WaitTest {private static Object object = new Object (); public static void main (String [] args) {Thread thread = new Thread () {@ Override public void run () {synchronized (object) {System. out. println (System. currentTimeMillis () + ":" + Thread. currentThread (). getName () + "start"); try {object. wait (); // wait for the current thread to enter (enter Object. wait Queue) and release the object lock} catch (InterruptedException e) {e. printStackTrace ();} System. out. println (System. currentTimeMillis () + ":" + Thread. currentThread (). getName () + "End of thread execution") ;}}; thread. start (); Thread thread_2 = new Thread () {@ Override public void run () {synchronized (object) {System. out. println (System. currentTimeMillis () + ":" + Thread. currentThread (). getName () + "start"); try {object. Y (); // random in Object. in the waitd queue, wake up a thread System waiting for the object lock. out. println (System. currentTimeMillis () + ":" + Thread. currentThread (). getName () + "wake up a waiting Thread"); Thread. sleep (10000);} catch (InterruptedException e) {e. printStackTrace () ;}}}; thread_2.start ();}}

Execution result:

1473306408730: Thread-0 Enter start 1473306408731: Thread-1 Enter start 1473306408731: Thread-1 wake up a waiting Thread 1473306418731: Thread-0 Thread execution ends

From the timestamp, we can see that after Thread-1 notifies Thread-0 to continue execution, Thread-0 does not execute immediately, but waits for Thread-1 to release the Object lock, the Object lock can be re-acquired before execution can continue. (The last two timestamps subtract exactly 10 seconds)

 

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.