本文主要是介绍Balking(犹豫)设计模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
多个线程监控某个共享变量,A线程监控到共享变量发生变化后即将触发某个动作,但此时发现有另外一个线程B已经针对该变量的变化开始了行动,因此A便放弃了准备开始的工作,我们把这样的线程交互称为Balking(犹豫)设计模式。Balking模式在日常开发中很常见,如在系统资源的加载或者某些数据初始化时,在整个系统生命周期中,资源可能只被加载一次,可以采用这种模式解决,代码如下:
import java.util.Map;public class Balking {
private volatile boolean loaded=false;
private Map<String,String> resource;
public synchronized Map<String,String> load(){
//balking
if(loaded) {
return resource;
}else {
//load resource
this.loaded=true;
return resource;
}
}
}
具体的样例代码,以文档的自动保存和手动保存为例:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class Document {
private boolean changed=false;
private List<String> content=new ArrayList<>();
private final FileWriter writer;
private static AutoSavedThread autoSavedThread;private Document(String documentPath,String documentName) throws IOException {
this.writer=new FileWriter(new File(documentPath,documentName));
}public static Document create(String documentPath,String documentName) throws IOException {
Document document=new Document(documentPath,documentName);
autoSavedThread=new AutoSavedThread(document);
autoSavedThread.start();
return document;
}public void edit(String content) {
synchronized(this) {
this.content.add(content);
this.changed=true;
}
}public void close() throws IOException {
autoSavedThread.interrupt();
writer.close();
}public void save() throws IOException {
synchronized(this) {
if(!changed) {
return ;
}
System.out.println(Thread.currentThread()+" execute the save action");
for(String cacheLine:content) {
this.writer.write(cacheLine);
this.writer.write("\r\n");
}
this.writer.flush();
this.changed=false;
this.content.clear();
}
}
}
import java.io.IOException;
import java.util.concurrent.TimeUnit;public class AutoSavedThread extends Thread{
private final Document document;public AutoSavedThread(Document document) {
super("DocumentAutoSavedThread");
this.document=document;
}@Override
public void run() {
while(true) {
try {
document.save();
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
break;
}
}
}}
import java.io.IOException;
import java.util.Scanner;public class DocumentEditThread extends Thread{
private final String documentPath;
private final String documentName;
private final Scanner scnner=new Scanner(System.in);public DocumentEditThread(String documentPath,String documentName) {
super("DocumentEditThread");
this.documentPath=documentPath;
this.documentName=documentName;
}@Override
public void run() {
int times=0;
Document document;
try {
document = Document.create(documentPath, documentName);
while(true) {
String text=scnner.next();
if("quit".equals(text)) {
document.close();
break;
}
document.edit(text);
if(5==times) {
document.save();
times=0;
}
times++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这篇关于Balking(犹豫)设计模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!