Java中的interrupt、interrupted和isInterrupted方法

一、interrupt

interrupt用于中断线程。调用该方法的线程的状态将会被设置为“中断状态”。
【注意】线程中断仅仅是设置线程的中断状态位,并不会停止线程。需要用户自己去监视线程的状态并作出处理

1、打断正常运行的线程

public class InterruptTest {
    public static void main(String[] args) {

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    System.out.println(i++);
                }
            }
        });
        t1.start();
        System.out.println("主线程正常运行中.....");
        System.out.println("打断t1线程");
        t1.interrupt();
        System.out.println("t1线程的打断标记:" + t1.isInterrupted());
        System.exit(0);
    }
}

在这里插入图片描述
【结论】t1线程被打断后,t1线程并没有中止,而是继续运行,只是将打断标记设置为true

2、使用interrupt打断sleep中的线程

使用interrupt打断sleep中的线程,会抛出异常,并且清除打断标记

private static void test1() throws InterruptedException {
        Thread t1 = new Thread(()->{
            sleep(1);
        }, "t1");
        t1.start();

        sleep(0.5);
        t1.interrupt();
        log.debug(" 打断状态: {}", t1.isInterrupted());
    }

在这里插入图片描述

3、使用interrupt打断join中的线程

使用interrupt打断join中的线程,会抛出异常,但是打断标记还是会置为true

private static void test11() throws InterruptedException {
        Thread t1 = new Thread(()->{
            int i = 0;
            while (i < 9999999) {
                i++;
            }
        }, "t1");
        Thread t2 = new Thread(()->{
            try {
                t1.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("等待t1线程执行结束");
        }, "t2");
        t1.start();
        t2.start();
        t2.interrupt();
        log.debug("t2打断状态: {}", t2.isInterrupted());
    }

在这里插入图片描述

4、打断 park 线程

之后补充,先了解下 park() 方法的使用

二、isInterrupted

查询当前线程的中断标志位是true还是false,利用中断标志位来结束线程

public class InterruptTest {
    public static void main(String[] args) {

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                // 当t1线程被打断后,条件不满足,t1线程执行结束
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println(i++);
                }
            }
        });
        t1.start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t1.interrupt();
    }
}

三、interrupted

interrupted是静态方法,查看线程中断信号是true还是false,并且清除中断信号;如果一个线程被中断了,第一次调用interrupted返回的是true,第二次调用则返回的是false

public class InterrupttedTest {
    public static void main(String[] args) {

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    System.out.println(i++);
                    System.out.println("查看线程的中断标志位:"+ Thread.interrupted());
                }
            }
        });
        t1.start();
        t1.interrupt();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t1.stop();
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值