【java】5-线程的中断

1. 线程中断的含义

当我们创建一个线程循环打印语句时,我们想要发出一个指令让该线程停下来,此时,我们可以发送线程中断的命令

线程是否中断实际上是根据一个表示是否中断的标志位来决定的,这个标志位我们既可以自定义,也可以使用Thread内部的一个boolean变量来表示

2. 自定义标志位

class MyRunnable implements Runnable {
    public volatile boolean isQuit = false;

    @Override
    public void run() {
        while (isQuit == false) {
            System.out.println("isQuit is false");
        }
        System.out.println("isQuit is true");
    }
}

public class Demo15 {
    public static void main(String[] args) throws InterruptedException {
        MyRunnable myRunnable = new MyRunnable();
        Thread t = new Thread(myRunnable);
        t.start();
        Thread.sleep(100);
        myRunnable.isQuit = true;
    }
}

3. 使用interrupt()方法

一、interrupt()中断对象关联的线程,如果线程正在阻塞,则以异常方式通知,否则设置标志位

public class Demo {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
           while (true) {
               System.out.println("hello");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   System.out.println(Thread.currentThread().isInterrupted());
                   e.printStackTrace();
                   System.out.println("thread is interrupted");
                   break;
               }
           }
        });
        t.start();
        Thread.sleep(3000);
        t.interrupt();
    }
}

二、Thread.interrupted()判断当前线程的中断标志位是否设置,调用后清除标志位

public class Demo {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            while (!Thread.interrupted()) {
                System.out.println("hello");
            }
            System.out.println(Thread.interrupted());
        });
        t.start();
        Thread.sleep(100);
        t.interrupt();
    }
}

三、Thread.currentThread().isInterrupted()判断对象关联的线程的标志位是否设置,调用后不清除标志位

public class Demo {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("hello");
            }
            System.out.println(Thread.currentThread().isInterrupted());
        });
        t.start();
        Thread.sleep(100);
        t.interrupt();
    }
}

故线程收到通知的方式有两种:

  1. 如果线程因为调用 wait/join/sleep 等方法而阻塞挂起,则以InterruptedException 异常的形式通知,清除中断标志
  2. 当出现 InterruptedException 的时候, 要不要结束线程取决于 catch 中代码的写法. 可以选择忽略这个异常, 也可以跳出循环结束线程.
  3. 否则,只是内部的一个中断标志被设置,thread 可以通过
    Thread.interrupted() 判断当前线程的中断标志被设置,清除中断标志
    Thread.currentThread().isInterrupted() 判断指定线程的中断标志被设置,不清除中断标志
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DanteIoVeYou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值