码迷,mamicode.com
首页 > 编程语言 > 详细

InterruptionInJava

时间:2018-11-30 13:57:55      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:ring   code   try   current   tar   row   pac   package   lse   

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");
          }
      }
  }
}

 

InterruptionInJava

标签:ring   code   try   current   tar   row   pac   package   lse   

原文地址:https://www.cnblogs.com/tonggc1668/p/10043217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!