In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
本文小编为大家详细介绍"java的Balking模式怎么实现",内容详细,步骤清晰,细节处理妥当,希望这篇"java的Balking模式怎么实现"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
Balk有拒绝,阻碍的意思。如果现在不适合执行这个操作,或者没必要执行这个操作,就停止处理,直接返回。这就是Balking模式。
Balking 模式可以和Guarded Suspension 模式对比,都存在守护条件。而在Balking模式中,如果守护条件不成立就立即中断处理,而Guarded Suspension 模式则是一直等待至可以运行。
创建4个类
名字说明Data表示可以修改并保存的数据的类SaverThread定期保存数据内容的类ChangerThread修改并保存数据内容的类Main测试类 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; } // 修改数据内容 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(); }}import java.io.IOException; public class SaverThread extends Thread{ private final Data data; public SaverThread(String fileName ,Data data) { super(fileName); this.data = data; } @Override public void run() { try { while (true) { data.save(); // 要求保存数据 Thread.sleep(1000); } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }}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(1000)); // 执行其他操作 ,只是随机暂停一段时间 data.save(); // 显式的保存 } } catch (Exception e) { e.printStackTrace(); } }}
这里注意Data类的doSava方法每次都是重新创建文件,文件内容会全部消失,该示例不能直接用作应用程序的自动保存功能,如果想做,必须对文件进行备份。
public class Main { public static void main(String[] args) { Data data = new Data("data.txt","(empty)"); new ChangerThread("ChangerThread",data).start(); new SaverThread("SaverThread",data).start(); }}
运行结果:没有出现重复的编号。
Balking 模式中的登场角色
GuardedObject (被保护的对象)
GuardedObject 角色是一个拥有被防护的方法(guardedMethod)的类。当线程执行guardedMethod方法时,若守护条件成立,则执行实际的处理。反之,直接返回。守护条件的成立与否会随着GuardedObject 角色的状态变化而改变。
除了guardedMethod方法外,GuardedObject应该有改变状态的方法(StateChangingMethod)。在上面示例中,由Data扮演此角色,sava方法则是guardedMethod,change方法则是StateChangingMethod。守护条件对应的是change属性为true;
使用场景:
1 并不需要执行时
比如写文件时,如果文件内容没有变化,则无需再写,提高程序性能。
2 不需要等待守护条件成立时
Balking模式的特点就是不进行等待,一旦守护条件不成立时,可以立即返回并进入下一个操作。这能够大大提高程序的相应性。
3 守护条件仅在第一次成立时
例如我们先看看下面的代码
public class Something { private boolean initialized =false; public synchronized void init(){ if (initialized){ return; } doInit(); initialized = true; } private void doInit(){ // 处理逻辑 }}
initialized 表示初始化是否完成,而这里一旦初始化完成,initialized 就为true,而且状态就永远不会发生变化了。所以守护条件不成立时,直接返回。像这种initialized 字段,状态仅变化一次的变量,我们通常称为闭锁。一旦把门锁上了,就再也打不开了。
balk结果的表示方式:当从guardedMethod方法中balk并返回时,有如下表示方式
忽略balk通过返回值来表示balk,如true,false可以通过 异常的方式来表示
读到这里,这篇"java的Balking模式怎么实现"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.