Java multi-thread wake-up and java multi-thread wake-up
1 package TestThread. threadSynchronized. testInterruptedException; 2 3 public class InterruptDemo {4 public static void main (String [] args) {5 TestWait t = new TestWait ("thread 1 "); 6 TestWait t1 = new TestWait ("thread 2"); 7 Test te = new Test ("thread 3", t); 8 t. start (); 9 t1.start (); 10 te. start (); 11} 12} 13 14 class TestWait extends Thread {15 String name; 16 17 public TestWait (String name) {18 super (name); 19 This. name = name; 20} 21 22 public synchronized void run () {23 for (int I = 0; I <5; I ++) {24 if (I = 4) {25 try {26 // release the current lock immediately after waiting and enter the waiting pool to wait for awakening 27 // when the thread in the waiting pool is awakened, statement 28 after executing this statement again this. wait (); 29 System. out. println (Thread. currentThread (). getName () + ": I have not been executed yet! "); 30} catch (InterruptedException e) {31 e. printStackTrace (); 32} 33} 34 System. out. println (Thread. currentThread (). getName () + ": the current value is --->" + I); 35} 36} 37} 38 39 class Test extends Thread {40 private TestWait testwait; 41 String name; 42 43 public Test (String name, TestWait testwait) {44 super (name); 45 this. name = name; 46 this. testwait = testwait; 47} 48 49 public void run () {50 synchronized (testwait) {51 testwait. Y (); // after this method is called, the specified object will be awakened and the subsequent operation will continue after waking up. 52} 53} 54}
The execution result is: