Package Org.rui.thread.block;import Java.util.timer;import Java.util.timertask;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.TimeUnit; public class Notifyvsnotifyall {public static void main (string[] args) throws interruptedexception {Executorservice exec = Executors.newcachedthreadpool (); for (int i = 0; i < 5; i++) {Exec.execute (New Task ());} Exec.execute (New Task2 ()); Timer timer = new timer ();//Schedule the specified task to begin repeating a fixed rate of execution after the specified delay. Timer.scheduleatfixedrate (New TimerTask () {Boolean prod = true;public void run () {if (prod) {System.out.println ("\ n notif Y () "); Task.blocker.prod ();//prod = false;} else {System.out.println ("\ n notifyall ()"); Task.blocker.prodAll ();p Rod = True;}}, 400, 400); TimeUnit.SECONDS.sleep (5);//Run for a while...timer.cancel ();//Terminate this timer, System.out.println ("\ n Timer canceled"); TimeUnit.MILLISECONDS.sleep (500); System.out.println ("Task.blocker.prodAll ()"); Task2.blocker.prodAll ();//wake-up Task2 does not include any locks waiting in the Task.blockertask of TimeUnit.MILLISECONDS.sleep (500); System.out.println ("\nshutting down"); Exec.shutdownnow ();}} class Blocker {synchronized void Waitingcall () {try {while (! Thread.interrupted ()) {wait (); System.out.println (Thread.CurrentThread () + "");}} catch (Interruptedexception e) {//OK to exit this way}}synchronized void prod () {notify ();//In many of the tasks waiting for the same lock, only one will be awakened, so if you If you want to use nofify, you must ensure that the appropriate task}synchronized void Prodall () {Notifyall () is awakened;//Only the task waiting for this lock will be awakened}}//////////class task Implements Runnable {static blocker blocker = new blocker ();p ublic void Run () {Blocker.waitingcall ();}} Class Task2 implements Runnable {//A separate blocker object a separate Interceptor object static blocker blocker = new blocker (); public void Run () {blocker.waitingcall ();//system.out.println (Thread.CurrentThread () + ": Task will not be awakened Task2");}} /** * Output: * Notify () Thread[pool-1-thread-2,5,main] Notifyall () Thread[pool-1-thread-2,5,main] thread[ Pool-1-thread-5,5,main] Thread[pool-1-thread-4,5,main] Thread[pool-1-thread-3,5,main] ThreAd[pool-1-thread-1,5,main] Notify () Thread[pool-1-thread-2,5,main] Notifyall () Thread[pool-1-thread-2,5,main] Thread[pool-1-thread-1,5,main] Thread[pool-1-thread-3,5,main] Thread[pool-1-thread-4,5,main] Thread[ Pool-1-thread-5,5,main] Notify () Thread[pool-1-thread-2,5,main] Notifyall () Thread[pool-1-thread-2,5,main] Thread[ Pool-1-thread-5,5,main] Thread[pool-1-thread-4,5,main] thread[pool-1-thread-3,5,main] Thread[pool-1-thread-1,5, Main] Notify () Thread[pool-1-thread-2,5,main] Notifyall () Thread[pool-1-thread-2,5,main] thread[pool-1-thread-1,5, Main] thread[pool-1-thread-3,5,main] Thread[pool-1-thread-4,5,main] Thread[pool-1-thread-5,5,main] Notify () Thread[ Pool-1-thread-2,5,main] Notifyall () Thread[pool-1-thread-2,5,main] Thread[pool-1-thread-5,5,main] Thread[ Pool-1-thread-4,5,main] Thread[pool-1-thread-3,5,main] Thread[pool-1-thread-1,5,main] Notify () Thread[ Pool-1-thread-2,5,main] Notifyall () Thread[pool-1-thread-2,5,main] Thread[pool-1-thread-1,5,main] Thread[ Pool-1-thread-3,5,mAin] Thread[pool-1-thread-4,5,main] thread[pool-1-thread-5,5,main] Timer canceledTask.blocker.prodAll () thread[ Pool-1-thread-6,5,main] Shutting down*/
Java thread missed signals, notify () and Notifyall use