InterruptionInJava

package com.test;

public class InterruptionInJava implements Runnable{
  
  public static void main(String[] args) throws InterruptedException {
      Thread testThread = new Thread(new InterruptionInJava(),"InterruptionInJava");
      //start thread
      testThread.start();
     
      //interrupt thread
      testThread.interrupt();

      System.out.println("main end");

  }

  @Override
  public void run() {
    try {
      Thread.sleep(10000);
    } catch (InterruptedException e) {
      System.out.println(Thread.currentThread().isInterrupted());
      Thread.currentThread().interrupt();
      System.out.println(Thread.currentThread().isInterrupted());
    }
      while(true){
          if(Thread.currentThread().isInterrupted()){
              System.out.println("Yes,I am interruted,but I am still running");

          }else{
              System.out.println("not yet interrupted");
          }
      }
  }
}

 

posted @ 2018-11-30 11:34  tonggc1668  阅读(143)  评论(0编辑  收藏  举报