Thread中interrupt使用方法

栏目: Java · 发布时间: 6年前

内容简介:在基于上面的理由,Java提供了新的中断机制(interrupt),其他线程调用想要终止线程的被调用线程不处于阻塞的时候,需要调用方法来监控标志.

合理中断线程

合理中断

Thread 类中,提供了 stop() , suspend()resume() 方法,这三个方法分别是用来结束,暂停,恢复线程. 但是都已经被标记为@Deprecated废弃了. 因为一个线程不应该由其他线程来结束,他应该收到别人的通知,然后自己在合适的位置结束,如果不合理的结束,会导致很多意外的结果,比如临界区还没完全操作完,提前释放锁,但是部分状态已经改变,还有没有做一些清理操作等等.

基于上面的理由,Java提供了新的中断机制(interrupt),其他线程调用想要终止线程的 interrupt() 方法. 这个时候线程会根据自己的状态做出响应:

InterruptedException

被调用线程处于阻塞状态

public static void main(String[] args) {
    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                // 被调线程阻塞自己30s
                sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    try {
        // 启动线程
        thread.start();
        // 主线程阻塞自己3秒
        TimeUnit.SECONDS.sleep(3);
        // 中断线程
        thread.interrupt();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
// ↓ out ↓
// java.lang.InterruptedException: sleep interrupted
// ...

被调用线程处于正常运行

被调用线程不处于阻塞的时候,需要调用方法来监控标志.

public static void main(String[] args) {
    Thread thread = new Thread() {
        @Override
        public void run() {
            while (!Thread.interrupted()) {
                System.out.println("我还稳得住...");
            }
        }
    };
    try {
        thread.start();
        TimeUnit.SECONDS.sleep(3);
        thread.interrupt();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

// ↓ out ↓
// 我能稳得住
// ...

该程序会在检测interrupt标志,如果发现interrupt标志设置为true,则会结束自己.

interrupted()和isInterrupt()的区别

区别: 是否会清除interrupt标志. isInterrupt()方法不会改变标志,而interrupted()方法会在检测的同时,如果发现标志为true,则会返回true,然后把标志置为false.

public static void main(String[] args) {
    Thread thread = new Thread() {
        @Override
        public void run() {
            while (!Thread.interrupted()) {
                System.out.println("我还稳得住...");
            }
            // :warning::warning:添加下面代码:warning::warning:
            System.out.println(Thread.interrupted());
        }
    };
    try {
        thread.start();
        TimeUnit.SECONDS.sleep(3);
        thread.interrupt();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

// ↓ out ↓
// 我还稳得住...
// ...(省略)
// false

上面实例说明Thread.interrupted()方法会在标志为true的情况下修改interrupted的标志.

public static void main(String[] args) {
    Thread thread = new Thread() {
        @Override
        public void run() {
            // :warning::warning:修改方法:warning::warning:
            while (!isInterrupted()) {
                System.out.println("我还稳得住...");
            }
            System.out.println(Thread.interrupted());
        }
    };
    try {
        thread.start();
        TimeUnit.SECONDS.sleep(3);
        thread.interrupt();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

// ↓ out ↓
// 我还稳得住...
// ...(省略)
// true

源码解析

Thread中interrupt使用方法

通过观察源码可以看出interrupted方法最后会调用isInterrupted(true)方法,而传入的参数代表是否清除标志位. 可以看到isInterrupted(boolean)是一个本地方法,最终会通过C/C++来执行. 而isInterrupted()最后传入的参数为false,说明不清除标志位.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

决战618:探秘京东技术取胜之道

决战618:探秘京东技术取胜之道

京东集团618作战指挥中心 / 电子工业出版社 / 2017-11 / 99

《决战618:探秘京东技术取胜之道》以京东技术团队备战618为主线,集合京东数百位技术专家,对京东所有和618相关的关键技术系统进行了一次全面的梳理和总结,是京东技术体系的智慧结晶。 《决战618:探秘京东技术取胜之道》从前端的网站、移动入口到后端的结算、履约、物流、供应链等体系,系统展示了京东最新的技术成就。同时,也涵盖了京东正在充分运用大数据、人工智能等先进技术对所有技术体系架构进行整体......一起来看看 《决战618:探秘京东技术取胜之道》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器