Java multi-thread wake-up and java multi-thread wake-up

Source: Internet
Author: User

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:

 

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.