Busy waiting does not make efficient use of the CPU running the waiting thread (and busy waiting for the CPU to be too scary, use caution) unless the average wait time is very short. Otherwise, it is wiser to let the waiting thread go to sleep or non-running until it receives the signal it waits for.
Java has a built-in wait mechanism to allow threads to become non-operational when waiting for a signal. The Java.lang.Object class defines three methods, wait (), notify (), and Notifyall () to implement this wait mechanism.
However, you must obtain the lock on the object using Wait (), notify (), and Notifyall (), otherwise the exception will be thrown illegalmonitorstateexception!
Wait (): Sleeps the current thread.
Notify (): Randomly wakes up a thread that is waiting for the current object and cannot specify which one to wake.
Notify (): Wakes all threads that are waiting on the current object.
Note (Sync block):
1. When a thread is awakened, it does not immediately exit the wait () method to execute the next code until the thread that calls the Notify () method exits its own synchronization block.
2. After the same use of the Notifyall () method, not all threads immediately exit the Wait () method, and the thread that obtains the lock on the object exits one after the other.
Importorg.junit.Test;Import StaticJava.lang.Thread.sleep; Public classmywaitnotify { Public classmonitor{} Monitor monitorobject=NewMonitor (); BooleanWaitsign =false; Public voiddowait () {synchronized(monitorobject) { while(!waitsign) { Try{monitorobject.wait (); } Catch(interruptedexception e) {e.printstacktrace (); } } } } Public voiddonotify () {synchronized(monitorobject) {waitsign=true; Monitorobject.notify (); } } Public voidDonotifyall () {synchronized(monitorobject) {waitsign=true; Monitorobject.notifyall (); }} @Test Public voidtestnotify () {NewThread () { Public voidrun () {Try{System.out.println ("Thread 1: Start"); Sleep (5000); System.out.println ("Thread 1:notify;"); Donotify (); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("Child Thread End"); }}.start (); System.out.println ("Main thread Wait"); Dowait (); System.out.println ("Main thread is activated"); } @Test Public voidTestnotifyall () {NewThread () { Public voidrun () {Try{System.out.println ("Thread 1: Start"); Sleep (5000); System.out.println ("Thread 1:notifyall;"); Donotifyall (); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("Thread 1: End"); }}.start (); NewThread () { Public voidrun () {System.out.println ("Thread 2: Wait"); Dowait (); Try{sleep (3000); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("Thread 2: Activation"); System.out.println ("Thread 2: End"); }}.start (); System.out.println ("Main thread Wait"); Dowait (); System.out.println ("Main thread: Active"); Try{sleep (10000); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("Main thread: Complete"); }}
In the Mywaitnotify class:
In the 1.testNotify () method, the main thread wait is tested, and the child thread notify.
In the 2.testNotifyAll () method, the main thread wait is tested, the child thread is 2wait, and the child thread is 1notifyAll.
Note the problem that is prone to occur:
The 1.notify () method executes before the wait () method, which causes the thread that calls the wait () method to always wait and not wake up.
WORKAROUND: Store the Notify () signal through a variable, which must be detected before any one thread needs to wait () to ensure that there is no notify to wait.
2. Calling wait () waits for a thread that may have a "false wake" condition, that is, a thread that calls wait () automatically wakes up without notify ().
Workaround: The Notify () signal variable is detected through the while loop, and if the Notify () signal variable indicates that no thread has performed the Notify () then the description is indeed "false wakeup" and you need to continue with wait ().
Public void dowait () { synchronized (monitorobject) { while ( ! Waitsign) { try { monitorobject.wait (); Catch (interruptedexception e) { e.printstacktrace (); }}}}
Reference Document: Http://wiki.jikexueyuan.com/project/java-concurrent/thread-communication.html
Java multithreading--wait (), notify (), Notifyall () usage