设计模式-犹豫模式(Balking)

Balking模式用在一个线程发现另一个线程或本地线程在重复做某件相同的事,那么就让本地线程不用再做了直接结束返回。

class MonitorService{
    private Thread t1;
    private boolean i;
    private boolean n=false;
    public void start(){
        synchronized (this){
            if(n){
                return;
            }
            n=true;
        }
        t1=new Thread(()->{
            i=true;
            while (i){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    		System.out.println("监控线程启动");
            }
        });
        t1.start();
    }

    public void stop(){
        i=false;
        t1.interrupt();
    }

}
posted @   csm~/  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示