理解Java的Thread中的Interrupt机制

看了这个博客后,茅舍顿开的,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
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值