Balking模式

 

适用的情况

不想让各个线程随意的访问实例, 保持实例的安全性, 但是又想在保证安全性同时提高响应速度.

实现的方式

如果实例处于非正确的状态, 不等待实例恢复正确, 而是直接返回.

  • 判断检测实例的状态是不正确的.
    • 通过return语句返回.
    • 通过throw抛出一个异常中断执行.

      相关的模式

  • 如果线程需要等待正常的数据返回, 那么可以使用Guarded Suspension模式.

    代码示例:

    这是一个每隔一段时间自动保存数据的代码, 如果内容没有变化, 那么保存数据的线程可以直接return,无需等待数据的更改之后进行保存,然后再返回.

package com.graphic.balking;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public 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){
        content = newContent;
        changed = true;
    }

    public synchronized void save() throws IOException{
        if (!changed){
            return;
        }
        doSave();
        changed = false;
    }

    private void doSave() throws IOException{
        System.out.println(Thread.currentThread().getName() + "Calls doSave , content = " + content);
        Writer writer = new FileWriter(fileName);
        writer.write(content);
        writer.close();
    }
}

package com.graphic.balking;

import java.io.IOException;

public class SaverThread extends Thread{
    private final Data data;

    public SaverThread(String name, Data data){
        super(name);
        this.data = data;
    }

    @Override
    public void run() {
        try {
            while (true){
                data.save();
                Thread.sleep(1000);
            }
        }catch (IOException | InterruptedException e){
            e.printStackTrace();
        }
    }
}

package com.graphic.balking;

import java.io.IOException;
import java.util.Random;

public class ChangerThread extends Thread{
    private final Data data;
    private final 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(10000));
                data.save();
            }
        }catch (IOException | InterruptedException e){
            e.printStackTrace();
        }
    }
}

package com.graphic.balking;

public class Main {
    public static void main(String[] args) {
        Data data = new Data("balkingTest", "this si first");
        new ChangerThread("changer-1", data).start();
        new SaverThread("saver-1", data).start();
    }
}