线程中断 interrupted()、interrupt()、isInterrupted()方法用法及详解

一、设置标志位
  • 缺点:当try catch中sleep()较长时间时,不能及时中断。
  • 本应该在3秒之后就及时中断,但在程序处于阻塞状态,没有中断
 class StopThreadTest1 {
    private volatile static boolean IS_STOP;

    public static void main(String[] args) throws InterruptedException {
        new Thread(()->{
            while(!IS_STOP){
                System.out.println(Thread.currentThread().getName());
                try {
                    Thread.sleep(1000000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        Thread.sleep(3000L);
        IS_STOP=true;
    }
}
二、在子线程sleep阻塞的时候,中断它。
  1. 线程初始时,中断标志位=false
  2. 主线程休眠3秒,子线程执行3秒后处于阻塞状态
  3. 调用线程的interrupt()方法,该线程的中断标志位=true,
  4. 线程处于阻塞态,中断抛出InterruptedException(该异常在while循环体外被捕获),因此循环被终止。
public class InterruptThread {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                while (!Thread.interrupted()) {
                    System.out.println(Thread.currentThread().getName());
                        //sleep线程阻塞时,也可以中断,抛出异常sleep interrupted
                        Thread.sleep(1000000L);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t.start();
        Thread.sleep(3000L);
        t.interrupt();
    }
}
运行结果:

在这里插入图片描述

通过 thread 对象调用 interrupt() 方法通知该线程停止运行。
thread 收到通知的方式有两种:
1. 如果线程调用了 wait/join/sleep 等方法而阻塞挂起,则以 InterruptedException 异常的形式通知,清除中断标志.
2. 否则,只是内部的一个中断标志被设置,thread 可以通过
(1) Thread.interrupted() 判断当前线程的中断标志被设置,清除中断标志
(2) Thread.currentThread().isInterrupted() 判断指定线程的中断标志被设置,不清除中断标志
三、在while循环体内进行阻塞
  1. 线程初始时,中断标志位=false
  2. 调用线程的interrupt()方法,该线程的中断标志位=true
  3. 如果线程处于阻塞态,中断抛出InterruptedException时(该异常在while循环体内被捕获),会重置线程的中断标志位(即interrupted()会返回false)
class InterruptThread1 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            while (!Thread.interrupted()) {
                try {
                    System.out.println(Thread.currentThread().getName());
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    //线程阻塞的时候,抛出InterruptedException中断线程,isInterrupted=false;
                    e.printStackTrace();
                }
            }
        });
        t.start();
        Thread.sleep(3000L);
        t.interrupt();
    }
}
运行结果:

在这里插入图片描述
解决方法:在捕获异常时,额外的进行退出while循环的处理。
例如,在catch(InterruptedException)中添加break或return就能解决该问题。

四、Thread.interrupted()作用
class InterruptThread2 {
    public static void main(String[] args)  {
        Thread t=new Thread(()->{
            //Thread.interrupted():返回当前的中断标志位,并重置标志位
            //(1)boolean tmp = isInterrupted;
            //(2)isInterrupted=false;
            //(3)return tmp;
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.interrupted());
            }
        });
        t.start();
        t.interrupt();
    }
}
运行结果:

在这里插入图片描述

五、线程对象.isInterrupted()作用:只返回中断标志位,不做任何修改
class InterruptThread3 {
    public static void main(String[] args)  {
        Thread t=new Thread(()->{
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().isInterrupted());
            }
        });
        t.start();
        t.interrupt();
    }
}
运行结果:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值