Balking Design Pattern
作者:互联网
文章目录
前言
想一下,日常生活中,会有这样一个场景:去海底捞吃饭,海底捞的服务服务员都是服务很周到、很热情的。你的酒杯里没有酒了,他会自动上去服务。可能会有多个服务为你服务,当一个服务员服务完后离开了,这时另一个服务员经过,看到你的酒杯里的酒是满的。这时他就不会再向你服务了。与这个生活场景类似,多线程设计模式里有这么一个模式:Balking Design Pattern
一、Balking Design Pattern概述
Balking Design Pattern译为放弃设计模式,大概意思就是,多个线程服务干通一件事。如果事情的当前状态为不需要改变,那干这件事的线程看到这种状态后自动放弃干这件事。这就是Balking Design Pattern
二、代码示例
1.共享资源
多个线程访问共享资源,要改变共享资源的内容,然后保存共享资源的内容到文件里。所以首先共享资源要有内容字段、文件路径、状态标志位。要提供改变内容的方法、保存内容的方法。代码如下:
package balking;
import java.io.FileWriter;
import java.io.IOException;
public class BalkingData {
private final String filePath;
private String content;
private volatile boolean needChange;
public BalkingData(String filePath, String content) {
this.filePath = filePath;
this.content = content;
this.needChange = true;
}
public void change(String content) {
this.content = content;
this.needChange = true;
}
public void save() throws IOException {
if (!needChange) {
return;
}
doSave();
this.needChange = false;
}
private void doSave() throws IOException {
System.out.println(Thread.currentThread().getName() + " call to save, content = " + content);
try (FileWriter writer = new FileWriter(this.filePath, true)){
writer.write(content);
writer.write("\n");
writer.flush();
}
}
}
2.顾客线程(消费者)
要改变共享资源的状态,比如不断地喝掉酒杯中的酒,可以叫服务员添酒,也可以自己来。添酒过程对应代码的保存方法。代码如下
package balking;
import java.io.IOException;
import java.util.Random;
public class ConsumerThread extends Thread {
private final BalkingData balkingData;
private final Random random = new Random(System.currentTimeMillis());
public ConsumerThread(BalkingData balkingData) {
super("Consumer");
this.balkingData = balkingData;
}
@Override
public void run() {
try {
balkingData.save();
for (int i = 0; i < 20; i++) {
balkingData.change("NO." + i);
Thread.sleep(random.nextInt(1000));
balkingData.save();
}
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
}
3.服务员线程
服务员要不断地添酒以服务消费者。不断地调用save方法。
package balking;
import java.io.IOException;
public class WaiterThread extends Thread {
private final BalkingData balkingData;
public WaiterThread(BalkingData balkingData) {
super("Waiter");
this.balkingData = balkingData;
}
@Override
public void run() {
while (true) {
try {
balkingData.save();
Thread.sleep(1_000L);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
}
4.客户端
package balking;
public class client {
public static void main(String[] args) {
BalkingData balkingData =
new BalkingData("C:\\Users\\xxx\\Desktop\\test.txt", "=======begin======");
new ConsumerThread(balkingData).start();
new WaiterThread(balkingData).start();
}
}
5.执行结果
/**
* 控制台打印结果
*/
Consumer call to save, content = =======begin======
Waiter call to save, content = =======begin======
Waiter call to save, content = NO.1
Waiter call to save, content = NO.2
Consumer call to save, content = NO.3
Consumer call to save, content = NO.4
Waiter call to save, content = NO.5
Consumer call to save, content = NO.6
Waiter call to save, content = NO.7
Waiter call to save, content = NO.8
Consumer call to save, content = NO.9
Waiter call to save, content = NO.10
Waiter call to save, content = NO.11
Consumer call to save, content = NO.12
Consumer call to save, content = NO.13
Waiter call to save, content = NO.14
Consumer call to save, content = NO.15
Waiter call to save, content = NO.16
Waiter call to save, content = NO.17
Consumer call to save, content = NO.18
Consumer call to save, content = NO.19
/**
* 文件内容
*/
=======begin======
=======begin======
NO.1
NO.2
NO.3
NO.4
NO.5
NO.6
NO.7
NO.8
NO.9
NO.10
NO.11
NO.12
NO.13
NO.14
NO.15
NO.16
NO.17
NO.18
NO.19
标签:balkingData,Waiter,Pattern,content,Design,call,save,public,Balking 来源: https://blog.csdn.net/madleep/article/details/120555793