首页 > 编程语言 > 详细

多线程设计模式:两阶段终止模式

时间:2020-09-15 14:21:24      阅读:85      评论:0      收藏:0      [点我收藏+]

流程图:
技术分享图片

第一版(后续会优化)

@Slf4j
public class TwoPhaseTermination {

    private Thread monitor;

    public void start(){
        monitor = new Thread(()->{
            Thread current = Thread.currentThread();
            while(true){

                if(current.isInterrupted()){
                    log.info("after handler");
                    break;
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                    log.info("monitor");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    current.interrupt();
                }
            }

        });
        monitor.start();
    }

    public void stop(){
        monitor.interrupt();
    }
}
class Inner{
    public static void main(String[] args) throws InterruptedException {
        TwoPhaseTermination termination = new TwoPhaseTermination();
        termination.start();
        TimeUnit.SECONDS.sleep(5);
        termination.stop();
    }
}

多线程设计模式:两阶段终止模式

原文:https://www.cnblogs.com/wwjj4811/p/13671674.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!