线程中断.

上一篇:yield()–让出CPU执行权

线程中断

线程中断是一种线程中的协作模式,通过设置线程的中断标志并不能直接终止线程的执行,而是被中断的线程根据中断状态自行处理。

中断线程方法:interrupt()
     仅仅是给调用这个方法的线程设置一个中断标志,实际上并没有立即被中断,调用这个方法的线程还会继续往下执行;如果A线程调用了wait、sleep、join等方法阻塞了自己,B线程在A阻塞期间调用了A的interrupt()方法,这个时候A会抛出InterruptedException并返回。

判断线程中断状态:isInterrupted()
    检测当前被调用线程是否被中断,如果是返回true

检测当前线程是否被中断:interrupted()
    检测当前调用线程是否被中断,是则返回true,与isInterrupted()不同的是,该方法如果发现当前线程被中断则会清除中断标志

看图理解👇
在这里插入图片描述

示例代码:

public class InterruptTest {

    //获取当前时间,精确到秒
    public static void nowTime(){
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        String now = format.format(date);
        System.out.println("当前时间:"+now);
    }

    /**
     * 测试调用interrupt()是否立即中断
     */
    public void test1(){
        Thread thread1 = new Thread(() -> {
            //当前线程没有被中断的话就一直执行
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("线程1   执行");
            }
            System.out.println("线程1   中断状态:" + Thread.currentThread().isInterrupted());
            System.out.println("线程1   执行完毕");
        });

        Thread thread2 = new Thread(() -> {
            try {
                System.out.println("线程2   执行");
                System.out.println("线程2   中断线程1");
                //中断线程1
                thread1.interrupt();
                //为了查看调用interrupt()后的逻辑  让线程2休眠五秒
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程2   执行完毕");
        });

        thread1.start();
        thread2.start();
    }

    /**
     * 测试A线程阻塞期间,B线程调用A的interrupt()方法
     */
    public void test2() {

        Thread thread1 = new Thread(() -> {
            try {
                //当前线程没有被中断的话就一直执行
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println("线程1   执行");
                    nowTime();
                    //让线程1休眠5秒
                    System.out.println("线程1   休眠5秒");
                    Thread.sleep(5000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            nowTime();
            System.out.println("线程1   中断状态:"+Thread.currentThread().isInterrupted());
            System.out.println("线程1   执行完毕");
        });

        Thread thread2 = new Thread(() -> {
            try {
            	//休眠1秒 让线程1先执行
            	Thread.sleep(1000);
                System.out.println("线程2   执行");
                System.out.println("线程2   中断线程1");
                //中断线程1
                thread1.interrupt();
                //为了查看调用interrupt()后的逻辑  让线程2休眠五秒
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程2   执行完毕");
        });

        thread1.start();
        thread2.start();
    }


    public static void main(String[] args) {
        InterruptTest interruptTest = new InterruptTest();
        interruptTest.test1();
        interruptTest.test2();
    }

}

test1()的执行结果:
在这里插入图片描述
线程2调用线程1的interrupt()后,线程1没有立即中断 还是在执行;

test2()执行结果:
在这里插入图片描述

根据test2()的逻辑,两次时间输出之间应该间隔5秒,实际上在线程2调用线程1的interrupt()方法时线程1就抛出异常返回了;说明,一个线程在阻塞期间如果被另外一个线程调用了它的interrupt()方法,那么它会从阻塞状态切换为运行状态;
这里只测试了sleep方法,其他阻塞线程的方法可以自行尝试。

总结:就是通过interrupt()给线程设置一个状态,线程再根据这个状态来执行逻辑,interrupt()的作用就好比一个开关,控制的是线程中断的状态。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值