天天看点

线程休眠示例Thread.sleep()

public class TestInterrupt {

  public static void main(String[] args) {

    MyThread thread = new MyThread();

    thread.start();

    try {

Thread.sleep(5000);

    }

    catch (InterruptedException e) {

     e.printStackTrace();

    }

    thread.interrupt();

  }

}

class MyThread extends Thread {

boolean flag = true;

  public void run(){

      try {

        sleep(10000);

      } catch (InterruptedException e) {

        System.out.println("hahahahaha");

      }

  }

}

主线程休眠5秒后interrupt正在休眠的子线程!interrupt后为什么程序就结束了呢?和前一篇矛盾了?