Examples of Java multi-thread wait, notify, and notifyall

Source: Internet
Author: User
Tags mutex sleep

In Java multi-threaded programming, wait () is used to bring the current thread into the blocking state, and notify () is used to wake the current thread and continue execution. Although they control the thread status, they are actually all methods in the Object, because the role of wait and consumer y is related to the mutex lock between threads.
Before executing wait () and notify (), you must obtain the mutex lock, that is, it must be used with synchronized. Wait () means to let out the obtained mutex lock and let yourself into the blocking status. The mutex lock has been obtained when notify () is used to wake up the current thread and continue execution.

If the lock object of synchronized is obj, correct use of wait and policy is obj. wait () and obj. Policy (). If you use this as the lock, you can directly write it as wait () and policy (). If the lock objects used before and after are inconsistent, IllegalMonitorStateException will occur.

When multiple threads use a mutex lock together, notify () will randomly select a thread that has executed wait () to wake up, and the rest will continue to be blocked. If you want to wake up all blocked processes, you can use policyall ().

Is it a bit dizzy? You can take the code and execute it. After reading the code, read the conclusion.

The code is as follows: Copy code
Package com. javaer. thread;
 
Public class Twait {
 
Public static void main (String [] args ){
TestThread testThread1 = new TestThread ();
TestThread testThread2 = new TestThread ();
TestThread testThread3 = new TestThread ();
 
TestThread1.start ();
TestThread2.start ();
TestThread3.start ();
 
System. out. println ("main thread sleep for 5 seconds ");
Try {
Thread. sleep (1000*5 );
} Catch (InterruptedException e ){
System. out. println ("main thread Interrupted ");
         }
 
System. out. println ("Wake Up Thread-0 ");
 
TestThread1.resumeByNotify ();
 
Try {
System. out. println ("The main thread sleep again ");
Thread. sleep (1000*5 );
} Catch (InterruptedException e ){
System. out. println ("Main Thread Interrupted ");
         }
 
System. out. println ("Wake up all By policyall ");
 
Testthread1.resumebypolicyall ();
 
 }
 
}
 
Class TestThread extends Thread {
 
Private static Object obj = new Object ();
 
@ Override
Public void run (){
System. out. println (getName () + "coming soon ");
 
Synchronized (obj ){
Try {
Obj. wait ();
} Catch (InterruptedException e ){
System. out. println (getName () + "Test Thread Interrupted ");
   }
  }
 
System. out. println (getName () + "wake up ");
 }
 
Public void resumeByNotify (){
Synchronized (obj ){
Obj. Y ();
  }
 }
 
Public void resumebypolicyall (){
Synchronized (obj ){
Obj. Policyall ();
  }
 }
}

Thread-0 is about to enter blocking
Thread-2 is about to enter blocking
Main thread sleep for 5 seconds
Thread-1 is about to enter blocking
Wake-Up Thread-0
The main thread sleep again
Thread-0 is awakened
Wake up all By policyall
Thread-1 awakened
Thread-2 awakened
In the above example, when the sub-thread starts, it will start to block and then the main thread will wake up one by one. If no thread is awake, this subthread will wait.

Testthread1.resumebypolicyall ();
Note that the program will be stuck there. The legendary zombie.

Thread-0 is about to enter blocking
Thread-2 is about to enter blocking
Main thread sleep for 5 seconds
Thread-1 is about to enter blocking
Wake-Up Thread-0
The main thread sleep again
Thread-0 is awakened
Wake up all By policyall
It is stuck here.

Conclusion
1. The current wait thread needs to enter the blocking status for some reason, that is, the thread is paused.
2. notify wake up a blocked thread that is wait
3. Yyall wakes up all blocked threads.

When wait is called, the thread automatically releases the object lock it occupies and does not apply for the object lock. When the thread is awakened, it obtains the right to get the object lock again.

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.