java中interrupter()两阶段终止模式

在一个线程T1中如何优雅终止线程T2?这里的【优雅】指的是给T2一个料理后事的机会。

package com.xzp.juc.test1;


import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Test6 {
    public static void main(String[] args) throws InterruptedException {
        TPTInterrupt tptInterrupt = new TPTInterrupt();
        log.debug("开始start");
        tptInterrupt.start();
        log.debug("结束start");
        Thread.sleep(3500);
        log.debug("开始stop");
        tptInterrupt.stop();
        log.debug("结束stop");
    }
}
@Slf4j
class TPTInterrupt{
    private Thread thread;
    public void start() {
        thread = new Thread(() -> {
            while (true) {
                Thread currented = Thread.currentThread();
                log.debug("1打断标记{}",currented.isInterrupted());
                if(currented.isInterrupted()){
                    log.debug("料理后事");
                    break;
                }
                try {
                    Thread.sleep(1000);//情况一,这个时候被打断会把打断标记编程假,所以还要执行一次 currented.interrupt();
                    log.debug("执行监控记录");//情况二,这个时候打断,不会将打断标记变成true,程序继续执行
                } catch (InterruptedException e) {
                    log.debug("2打断标记{}",currented.isInterrupted());
                    currented.interrupt();
                    log.debug("3打断标记{}",currented.isInterrupted());
                    e.printStackTrace();
                }
            }
        },"t1");
        thread.start();
    }
    public void stop() {
        log.debug("4打断标记{}",thread.isInterrupted());
        thread.interrupt();
        log.debug("5打断标记{}",thread.isInterrupted());
    }
}

在这里插入图片描述
执行逻辑

在这里插入图片描述

改进,加入volatile,实现JMM的可见性进行实现,可见性 - 保证指令不会受 cpu 缓存的影响。

@Slf4j
public class Test2 {
    public static void main(String[] args) throws InterruptedException {
        TwoPhaseTermination tptInterrupt = new TwoPhaseTermination();
        log.debug("开始start");
        tptInterrupt.start();
        log.debug("结束start");
        Thread.sleep(3500);
        log.debug("开始stop");
        tptInterrupt.stop();
        log.debug("结束stop");
    }
}
@Slf4j
class TwoPhaseTermination{
    private Thread monitorThread;
    private volatile boolean stop = false;
    public void start() throws InterruptedException{
        monitorThread =new Thread(() -> {
            while (true){
                if(stop){
                    log.debug("料理后事");
                    break;
                }
                try {
                    Thread.sleep(1000);
                    log.debug("执行监控记录");
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        },"monitor");
        monitorThread.start();
    }
    public void stop(){
        stop = true;
        //        防止try里面业务太长,直接打断,节省时间直接处理后事
        monitorThread.interrupt();
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iii我的天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值