Balking 设计模式

x33g5p2x  于2022-04-27 转载在 其他  
字(3.9k)|赞(0)|评价(0)|浏览(528)

一 点睛

多个线程监控某个变量,A 线程监控到共享变量发生变化后即将触发某个动作,但是此时发现另外一个线程 B 已经针对该变量的变化开始了行动,因此 A 便放弃了准备开始的工作,我们把这样的线程间交互称为 Balking(犹豫)设计模式。

其实这样的场景在生活中很常见,比如,你去饭店吃饭,吃到中途中想再点一个小菜,于是你举起手示意服务员,其中一个服务员看到你举手正准备过来的时候,发现距离你比较近的服务员已经准备要受理你的请求,于是中途放弃了。

再比如,我们用 word 编辑文档的时候,每次文字编辑都代表着文档状态发生了变化,除了我们可以使用 ctrl+s 快捷键手动保存以外,word 软件本身也会定期触发自动保存,如果 word 自动保存文档的线程在执行保存动作的时候,恰巧我们进行了主动保存,那么自动保存文档的线程将会放弃此时保存的动作。

简单的说,就是某个线程因为发现其他线程正在进行相同的工作而放弃即将开始的任务。

二 实战

1 文档类

  1. package concurrent.barking;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import static java.lang.Thread.currentThread;
  8. /**
  9. * @className: Document
  10. * @description: 代表正在编辑的文档类
  11. * @date: 2022/4/24
  12. * @author: cakin
  13. */
  14. public class Document {
  15. private final FileWriter write;
  16. // 一次需要保存的内容,可以将其理解为内容缓存
  17. private List<String> content = new ArrayList<>();
  18. // 如果文档发生改变,changed 会被设置为 true
  19. private boolean changed = false;
  20. // 自动保存文档的线程
  21. private static AutoSaveThread autoSaveThread;
  22. // 构造时需要传入文档保存的路径和文档的名称
  23. public Document(String documentPath, String documentName) throws IOException {
  24. this.write = new FileWriter(new File(documentPath, documentName), true);
  25. }
  26. // 静态方法,主要用于创建文档,顺便启动自动保存文档的线程
  27. public static Document create(String documentPath, String documentName) throws IOException {
  28. Document document = new Document(documentPath, documentName);
  29. autoSaveThread = new AutoSaveThread(document);
  30. autoSaveThread.start();
  31. return document;
  32. }
  33. // 文档编辑,其实就是往 content 队列中提交字符串
  34. public void edit(String content) {
  35. synchronized (this) {
  36. this.content.add(content);
  37. // 文档改变,change 会变为 true
  38. this.changed = true;
  39. }
  40. }
  41. // 文档关闭的时候首先中断自动保存线程,然后关闭 write 释放资源
  42. public void close() throws IOException {
  43. autoSaveThread.interrupt();
  44. write.close();
  45. }
  46. // 认为手动进行文档保存
  47. public synchronized void save() throws IOException {
  48. synchronized (this) {
  49. // balking 设计模式,如果文档已经保存了,则直接返回
  50. if (!changed) {
  51. return;
  52. }
  53. System.out.println(currentThread() + " execute the save action");
  54. // 将内容写入文档中
  55. for (String cacheLine : content) {
  56. this.write.write(cacheLine);
  57. this.write.write("\r\n");
  58. }
  59. this.write.flush();
  60. // 将 changed 修改为 false,表明此刻再没有新的内容编辑
  61. this.changed = false;
  62. }
  63. }
  64. }

2 自动保存文档线程

  1. package concurrent.barking;
  2. import java.io.IOException;
  3. import java.util.concurrent.TimeUnit;
  4. public class AutoSaveThread extends Thread {
  5. private final Document document;
  6. public AutoSaveThread(Document document) {
  7. super("DocumentAutoSaveThread");
  8. this.document = document;
  9. }
  10. @Override
  11. public void run() {
  12. while (true) {
  13. try {
  14. // 每隔一秒自动保存一次文档
  15. document.save();
  16. TimeUnit.SECONDS.sleep(1);
  17. } catch (IOException | InterruptedException e) {
  18. break;
  19. }
  20. }
  21. }
  22. }

3 手动编辑文档线程

  1. package concurrent.barking;
  2. import java.io.IOException;
  3. import java.util.Scanner;
  4. /**
  5. * @className: DocumentEditThread
  6. * @description: 主动编辑文档,除了对文档进行修改之外,还会同时按下 Ctrl + S 组合键(调用 save 方法)主动保存
  7. * @date: 2022/4/24
  8. * @author: cakin
  9. */
  10. public class DocumentEditThread extends Thread {
  11. private final String documentPath;
  12. private final String documentName;
  13. private final Scanner scanner = new Scanner(System.in);
  14. public DocumentEditThread(String documentPath, String documentName) {
  15. super("DocumentEditThread");
  16. this.documentPath = documentPath;
  17. this.documentName = documentName;
  18. }
  19. @Override
  20. public void run() {
  21. int times = 0;
  22. try {
  23. Document document = Document.create(documentPath, documentName);
  24. while (true) {
  25. // 获取用户的键盘输入
  26. String text = scanner.next();
  27. if ("quit".equals(text)) {
  28. document.close();
  29. break;
  30. }
  31. // 将内容编辑到 document 中
  32. document.edit(text);
  33. if (times == 5) {
  34. // 用户输入了 5 次之后进行文档保存
  35. document.save();
  36. times = 0;
  37. }
  38. times++;
  39. }
  40. } catch (IOException e) {
  41. throw new RuntimeException(e);
  42. }
  43. }
  44. }

4 测试

  1. package concurrent.barking;
  2. public class Test {
  3. public static void main(String[] args) {
  4. new DocumentEditThread("G:\\", "balking.txt").start();
  5. }
  6. }

三 测试结果

1

Thread[DocumentAutoSaveThread,5,main] execute the save action

2

Thread[DocumentAutoSaveThread,5,main] execute the save action

3

Thread[DocumentAutoSaveThread,5,main] execute the save action

4

Thread[DocumentAutoSaveThread,5,main] execute the save action

5

Thread[DocumentAutoSaveThread,5,main] execute the save action

6

Thread[DocumentEditThread,5,main] execute the save action

7

Thread[DocumentAutoSaveThread,5,main] execute the save action

8

Thread[DocumentAutoSaveThread,5,main] execute the save action

quit

四 说明

可以看到,自动保存线程和手动保存线程在分别进行文档保存,它们并不会在不改变内容的情况下执行 save 动作。

相关文章