结合interrupt state来理解interrupt

Thread类的interrupt方法倒是见过好几次,但是一直不知道他的具体是干什么的,今天查了以下api和core java大概解决了自己的疑惑。(顺便吐槽一下core java的翻译,问题实在不少)。

以下是我总结出来的几点,仅供参考,如有错误之处,敬请指出。

   1)线程的interrupt state可以通过Thread.currentThread.isInterrupted()来判断这个变量的真假,isInterrupt是一个实例方法,也可以通过线程实例来调用。

   There is boolean flag in every thread that indicating the interrupt state,When the interrupt method is called on a thread, the interrupted status of the thread is set.
   直译总感觉有点别扭,我还是意译一下吧:如果线程的interrupt方法被调用,那么线程的isInterrupted()返回值会为true,调用前是false;
   运行以下代码,可以看到程序的输出会出现这种情况:首先为false,interrupt以后,变为true,但是程序依旧会输出0到9;
   
   private void simpleInterrupt() {
       Thread t = new Thread(){
           @Override
           public void run() {
               for (int i = 0; i < 10; i++) {
                   System.out.println(i);
               }
           }
       };
   
       t.start();
       System.out.println("before interrupt:" + t.isInterrupted());
       t.interrupt();
       System.out.println("after interrupt:" + t.isInterrupted());
   }
   
   由此可见,interrupt并不像它的中文意思中断一样能中断线程,但是改变了isInterrupted方法的返回值,这个还是可以作为run方法的循环结束的条件,从而中断线程,代码如下:
   
   private void simpleInterruptAffect() {
       Thread t = new Thread(){
           @Override
           public void run() {
               for (int i = 0; i < 100 && !isInterrupted(); i++) {
                   System.out.println(i);
               }
           }
       };
   
       t.start();
       try {
           Thread.sleep(5);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       t.interrupt();
   }
   
   多运行几遍,发现i没到99就结束了。
   
   2)If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
       当线程由于sleep或者wait等方法阻塞时,如果调用了interrupt方法,就会抛出一个异常(补充:core java上说是由于系统无法检测处于阻塞状态线程的interrupt state),并且清除掉线程的interrupt status(使得isInterrupted方法返回false),用代码验证一下
   
           private void interruptSleep() {
             Thread t1 = new Thread() {
                 @Override
                 public void run() {
                     try {
                         Thread.sleep(1000);
                     } catch (InterruptedException e) {
                         System.out.println("interrupt status in catch:" + isInterrupted());
                     }
                 }
             };
             t1.start();
             System.out.println("interrupt status before interrupt:" + t1.isInterrupted());
             t1.interrupt();
             System.out.println("interrupt status after interrupt:" + t1.isInterrupted());
      
         }
      
   
      一般会依次输出  interrupt status before interrupt:false
               interrupt status after interrupt:true
               interrupt status in catch:false
      
      
   3)If you call the sleep method when the interrupted status is set, it doesn’t sleep. Instead, it
   clears the status and throws an InterruptedException.
   同样是意译:如果线程睡眠前线程它的isInterrupted返回true,线程就不会睡眠,相反,线程的会清除掉interrupt status并且抛出一个异常。同样用代码验证一下。
   
   volatile boolean flag = false;
   
   private void interruptThenSleep() {
       Thread t = new Thread(){
           @Override
           public void run() {
               while(!flag){}
               try {
                   Thread.sleep(10000);
               } catch (InterruptedException e) {
                   System.out.println("interrupt status in catch:" + isInterrupted());
               }
           }
       };
   
       t.start();
       t.interrupt();
       System.out.println("interrupt status after interrupt:" + t.isInterrupted());
       flag = true;
   }
   t线程不会休眠10s
   并且输出:interrupt status after interrupt:true
           interrupt status in catch:false
      
    结合2),可以得出结论:只要线程的isInterrupted()返回true,Thread.sleep()都会抛出异常,并且清除线程的中断状态。
   
    最后还有一个Thread类的静态方法interrupted,调用Thread.interrupted会清除掉当前线程的interrupt status,要注意该方法和isInterrupted的区别
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值