• 【面试:并发篇27:多线程:犹豫模式】


    【面试:并发篇27:多线程:犹豫模式】

    00.前言

    如果有任何问题请指出,感谢。

    01.介绍

    什么是犹豫模式,指的是一个线程发现另一个线程或者线程已经做了某一件相同的事,那么本线程就无需再做了,直接结束返回。
    通俗来说就是 一个对象只能有效调用一次方法,之后再调用直接返回结束。

    02.两阶段终止模式-犹豫模式优化

    @Slf4j(topic = "c.TwoPhaseTermination")
    public class Test13 {
        public static void main(String[] args) throws InterruptedException {
            TwoPhaseTermination tpt = new TwoPhaseTermination();
            tpt.start();
            tpt.start();
            tpt.start();
    
            Thread.sleep(3500);
            log.debug("停止监控");
            tpt.stop();
        }
    }
    
    @Slf4j(topic = "c.TwoPhaseTermination")
    class TwoPhaseTermination {
        // 监控线程
        private Thread monitorThread;
        // 停止标记
        private volatile boolean stop = false;
        // 判断是否执行过 start 方法
        private boolean starting = false;
    
        // 启动监控线程
        public void start() {
            synchronized (this) { //犹豫模式主要部分代码
                if (starting) { // false
                    return;
                }
                starting = true;
            }
            monitorThread = new Thread(() -> {
                while (true) {
                    if (stop) {
                        log.debug("料理后事");
                        break;
                    }
                    try {
                        Thread.sleep(1000);
                        log.debug("执行监控记录");
                    } catch (Exception e) {
                    }
                }
            }, "monitor");
            monitorThread.start();
        }
    
        // 停止监控线程
        public void stop() {
            stop = true;
            monitorThread.interrupt();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    结果

    19:53:25.992 c.TwoPhaseTermination [monitor] - 执行监控记录
    19:53:27.004 c.TwoPhaseTermination [monitor] - 执行监控记录
    19:53:28.009 c.TwoPhaseTermination [monitor] - 执行监控记录
    19:53:28.487 c.TwoPhaseTermination [main] - 停止监控
    19:53:28.487 c.TwoPhaseTermination [monitor] - 料理后事

    解释
    我们创建了一个starting变量 作用是判断这个方法是否已经被调用过 如果没有则执行之后的代码 如果有则直接返回,这里我们执行了三次start方法 但结果只运行了一次。
    不过还要注意一点 判断是否执行的if语句需要加锁,目的是为了防止 多线程情况下 对同一个方法反复执行

  • 相关阅读:
    java回收算法学习
    gwas数据根据eaf Z 和N 求beta和se
    设计模式系列-原型模式
    【智慧港口】NTP网络时间服务器助力数字化港口建设
    Google gtest事件机制
    41.【Java (基础入门-----三种结构体)】
    基于ARM的字符串拷贝实验(嵌入式系统)
    Eclipse嵌套项目部分项目丢失SVN源信息
    【云原生之Docker实战】使用Docker部署Flarum开源论坛
    uni-app + mui-player & vue + mui-player 播放flv文件
  • 原文地址:https://blog.csdn.net/m0_71229547/article/details/125994587