Balking Pattern不需要就不用做

本文介绍了一个关于Balking设计模式的应用实例,通过该模式避免了在多线程环境下重复保存文件的问题,并展示了如何确保一个初始化过程仅被执行一次。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

word自动保存功能,如果文档被修改了,后台线程每隔一段时间会自动执行保存功能,但是如果用户在自动保存之前用Ctrl+S手动保存呢?自动保存还会执行吗?答案是不会,因为这个操作时不需要重复做的。

 

public class BalkingTest {
    public static void main(String[] args) {
        Data data = new Data("data.txt","(empty)");
         
        new ChangerThread("ChangerThread", data).start();
        new AutoSaverThread("AutoSaverThread", data).start();
    }
}
 
class ChangerThread extends Thread{
    private Data data;
    private Random random = new Random();
     
    public ChangerThread(String name, Data data){
        super(name);
         
        this.data=data;
    }
 
    @Override
    public void run() {
        try{
            for(int i=0; true; i++){
                data.change("No." + i);
                Thread.sleep(random.nextInt(1000));
                data.save();
            }
        }
        catch(IOException e){
            e.printStackTrace();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}
 
class AutoSaverThread extends Thread{
    private Data data;
    public AutoSaverThread(String name, Data data){
        super(name);
         
        this.data=data;
    }
    @Override
    public void run() {
        try{
            while(true){
                data.save();
                Thread.sleep(3000);
            }
        }catch(IOException e){
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
class Data{
    private final String filename;
    private String content;
    private boolean changed;
     
    public Data(String filename, String content){
        this.filename=filename;
        this.content=content;
        this.changed = true;
    }
     
    public synchronized void change(String newContent){
        this.content = newContent;
        this.changed=true;
    }
     
    public synchronized void save() throws IOException{
        if(!changed){
            return;
        }
         
        doSave();
        changed=false;
    }
 
    private void doSave() throws IOException {
        System.err.println(Thread.currentThread().getName()+" call doSave(), content = "+ content);
         
        Writer writer = new FileWriter(filename);
        writer.write(content);
        writer.close();
    }
}

  

这样就不会重复保存了,执行结果如下:

AutoSaverThread call doSave(), content = No.0
ChangerThread call doSave(), content = No.1
ChangerThread call doSave(), content = No.2
ChangerThread call doSave(), content = No.3
ChangerThread call doSave(), content = No.4
AutoSaverThread call doSave(), content = No.5
ChangerThread call doSave(), content = No.6
ChangerThread call doSave(), content = No.7
ChangerThread call doSave(), content = No.8
ChangerThread call doSave(), content = No.9
ChangerThread call doSave(), content = No.10
AutoSaverThread call doSave(), content = No.11
ChangerThread call doSave(), content = No.12
ChangerThread call doSave(), content = No.13
ChangerThread call doSave(), content = No.14
ChangerThread call doSave(), content = No.15
AutoSaverThread call doSave(), content = No.16

  还有一种情况,就是一个类的一个方法只想被执行一次,我们可以用Balking 模式处理。

class RunOnceTime{
    private boolean initialized = false;
    public synchronized void init(){
        if(initialized == true){
            return;
        }
         
        doInit();
         
        initialized = true;
    }
     
    private void doInit(){
         
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值