理解Java的Thread中的Interrupt机制

来源:互联网 发布:网络教育学历学费贵 编辑:程序博客网 时间:2024/05/19 13:13

看了这个博客后,茅舍顿开的,http://m.blog.csdn.net/article/details?id=6941802

Java中线程安全的中断是使用interrupt()

运行中的线程不会因为interrupt()而中断,因为它仅仅是一个信号(status)

等待中的线程(wait(long),sleep(long),join(long))收到中断信号会抛出InterruptedException

静态方法Thread.interrupted()会清除中断标记(ClearInterrupted status),

以及线程终结了调用isInterrupted()都会返回false

package cn.cherish.mboot;/** * @author Cherish * @version Id: TestInterrupt.java, v 1 2017/3/24 10:19 Cherish Exp $$ */public class TestInterrupt {    public static void main(String[] args) {        Thread sleepThread = new Thread(() -> {            try {                System.out.println("【sleepThread】沉睡的线程" + Thread.currentThread().getName());                Thread.sleep(1000_00);            } catch (InterruptedException e) {//                e.printStackTrace();                System.out.println("【sleepThread】沉睡中收到中断信号,会抛出InterruptedException");            }finally {                System.out.println("【sleepThread】finally方法可以做些啥");            }            while(true){                System.out.println("【sleepThread】感谢中断信号,我不睡了");                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        });        Thread loopThread = new Thread(() -> {            while (!Thread.currentThread().isInterrupted()) {                System.out.println("【loopThread】 没收到中断信号,随便玩");            }            int count = 0;            while (Thread.currentThread().isInterrupted()) {                ++count;                System.out.println("【loopThread】 收到中断信号,改变操作方式,我可以选择释放资源,但是我不 " + count);                if (count == 10) {                    boolean beforeState = Thread.interrupted();                    System.out.println("【loopThread】 beforeState = " + beforeState);                    System.out.println("Thread.interrupted() 会重置中断标记为false");                    boolean nowState = Thread.currentThread().isInterrupted();                    System.out.println("【loopThread】 nowState = " + nowState);                    System.out.println("【loopThread】任性了10次,该结束了");                }            }            System.out.println("【loopThread】正式结束");        });        sleepThread.start();        loopThread.start();        try {            Thread.sleep(1);//等待其它两个线程的执行        } catch (InterruptedException e) {            e.printStackTrace();        }        sleepThread.interrupt();        loopThread.interrupt();        while (true) {            try {                Thread.sleep(1000);//等待其它两个线程收到中断信号            } catch (InterruptedException e) {                e.printStackTrace();            }            System.out.println("【sleepThread】 isInterrupted = " + sleepThread.isInterrupted());            System.out.println("【loopThread】 isInterrupted = " + loopThread.isInterrupted());        }    }}
0 0
原创粉丝点击