理解interrupt()方法

java interrupt()方法只是设置线程的中断标记,当对处于阻塞状态的线程调用interrupt方法时(处于阻塞状态的线程是调用sleep, wait, join 的线程),会抛出InterruptException异常,而这个异常会清除中断标记。因此,根据这两个思路,不同的run()方法设计,会导致不同的结果,下面是示例,并说明了运行了结果和原因

package com.concurrency;

import java.util.concurrent.TimeUnit;

public class Test{
    public static void main(String[] args) throws InterruptedException {
        RunTest rt = new RunTest();
        Thread t = new Thread(rt);
        t.start();
        TimeUnit.SECONDS.sleep(2);
        t.interrupt();
    }
}
//不同的run方法设计
class RunTest implements Runnable{
    //这种设计比较好,当调用阻塞操作时,会因为抛出异常退出,当不调用阻塞操作时,会因为检查中断状态而退出
    public void run1(){
        try{
            while(!Thread.interrupted()){
               // System.out.println("sleep 5s");
                //Thread.sleep(5000);接收到中断信号时,由于while循环判断不成立退出,不抛出异常
            }
            System.out.println("Exit normal");
        }catch(Exception e){
            System.out.println("interrupted");
        }
    }

    public void run5() {
        try{
            while(!Thread.interrupted()){
               // System.out.println("sleep 5s");
                Thread.sleep(5000);//接收到中断信号时,由于抛出异常退出,模拟耗时操作
            }
            System.out.println("Exit normal");
        }catch(Exception e){
            System.out.println("interrupted and exit");
        }
    }
    public void run3(){
        while(!Thread.interrupted()){
            //接收到中断信号时,由于while循环判断不成立退出
        }
        System.out.println("interrupt normal and exit 2");
    }
    //此种设计不好
    public void run4(){
        while(!Thread.interrupted()){
            try{
                TimeUnit.SECONDS.sleep(1);
                //接收到中断信号,捕获异常并清除中断状态,所以不退出,所以这种不是良好的设计方式,如果想要退出,需要在catch语句中Thread.currentThread().interrupt();
            }catch(Exception e){
                System.out.println("Interrupte and clear interrupt status, so run continue");
            }
        }
        System.out.println("exit normal 3");
    }
    public void run(){
        double d = 1;
        while(!Thread.interrupted()){
            while(d<3000){
                d = d + (Math.PI+Math.E)/d;
                System.out.println(d+ " running");
                //接收到中断信号时,不会中断正在运行的操作,只有当操作完成后,检查中断状态时会退出
            }
        }
        System.out.println("Exit "+d);
    }
}

可以将run()改为上面其中的一个,对比看看运行结果,如上面所述。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值