背景介绍:当现在不适合进行这个操作,或是没有必要进行这个操作时,就直接放弃进行这个操作。(此时也需要Guarded条件,但不需要wait/notify)
Balking范例,产生条件一个线程实例只会调用一个start,否则出现异常,该实现方式遵循Balking设计模式
public class Data {
private String filename; //修改是的名字
private String content; // 资料的内容
private boolean changed; //修改后的内容还没存储的话,值为true
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) { //守护条件
System.out.println(Thread.currentThread().getName() + " balks");
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();
}
}