定义
即 Guarded Suspension,用在一个线程等待另一个线程的执行结果
要点
- 有一个结果需要从一个线程传递到另一个线程,让他们关联同一个 GuardedObject
- 如果有结果不断从一个线程到另一个线程那么可以使用消息队列(见生产者/消费者)
- JDK 中,join 的实现、Future 的实现,采用的就是此模式
- 因为要等待另一方的结果,因此归类到同步模式
实现
@Slf4j
public class Test {
//线程1 等待 线程2 的下载结果
public static void main(String[] args) {
GuardedObject guardedObject = new GuardedObject();
new Thread(() -> {
log.debug("等待结果");
Object result = guardedObject.get();
log.debug("结果:{}", result);
}, "t1").start();
new Thread(() -> {
log.debug("执行下载");
String result = Test.download();
guardedObject.complete(result);
}, "t2").start();
}
/**
* 下载方法
*/
public static String download() {
try {
TimeUnit.SECONDS.sleep(3L);
return "下载成功";
} catch (InterruptedException e) {
e.printStackTrace();
}
return "下载失败";
}
}
class GuardedObject {
//结果
private Object result;
/**
* 永久等待
*
* @return Object
*/
public Object get() {
synchronized (this) {
//没有结果
while (result == null) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return result;
}
}
//产生结果
public void complete(Object result) {
synchronized (this) {
//给结果成员变量赋值
this.result = result;
this.notifyAll();
}
}
}
带超时版 GuardedObject
class GuardedObject {
//结果
private Object result;
/**
* 时效性等待
*
* @param timeout 等待时间-秒
* @return Object
*/
public Object get(long timeout) {
synchronized (this) {
//开始时间
long begin = System.currentTimeMillis();
//经历时间
long passedTime = 0;
while (result == null) {
long waitTime = timeout - passedTime;
//经历时间超过了最大等待时间,结束;
if (waitTime <= 0) {
break;
}
try {
this.wait(waitTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
passedTime = System.currentTimeMillis();
}
return result;
}
}
//产生结果
public void complete(Object result) {
synchronized (this) {
//给结果成员变量赋值
this.result = result;
this.notifyAll();
}
}
}
该设计模式运用于Join的底层源码实现:
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
进阶用法
Futures 就好比居民楼一层的信箱(每个信箱有房间编号),左侧的 t0,t2,t4 就好比等待邮件的居民,右 侧的 t1,t3,t5 就好比邮递员 如果需要在多个类之间使用 GuardedObject 对象,作为参数传递不是很方便,因此设计一个用来解耦的中间类, 这样不仅能够解耦【结果等待者】和【结果生产者】,还能够同时支持多个任务的管理
## GuardedObject 类:
class GuardedObject {
// 新增标识id
private final int id;
//结果
private Object result;
public GuardedObject(int id) {
this.id = id;
}
public int getId() {
return id;
}
/**
* 永久等待
*
* @return Object
*/
public Object get() {
synchronized (this) {
//没有结果
while (result == null) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return result;
}
}
/**
* 时效性等待
*
* @param timeout 等待时间-秒
* @return Object
*/
public Object get(long timeout) {
synchronized (this) {
//开始时间
long begin = System.currentTimeMillis();
//经历时间
long passedTime = 0;
while (result == null) {
long waitTime = timeout - passedTime;
//经历时间超过了最大等待时间,结束;
if (waitTime <= 0) {
break;
}
try {
this.wait(waitTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
passedTime = System.currentTimeMillis();
}
return result;
}
}
//产生结果
public void complete(Object result) {
synchronized (this) {
//给结果成员变量赋值
this.result = result;
this.notifyAll();
}
}
}
## Futures类
class Futures {
private static Map<Integer, GuardedObject> boxes = new ConcurrentHashMap<>();
private static int id = 1;
/**
* 生成id
*
* @return int
*/
public static synchronized int generatedId() {
return id++;
}
/**
* 创建 GuardedObject实例 方法
*
* @return GuardedObject
*/
public static GuardedObject createGuardedObject() {
GuardedObject go = new GuardedObject(generatedId());
boxes.put(go.getId(), go);
return go;
}
/**
* 获取 GuardedObject
* @param id GuardedObject id
* @return GuardedObject
*/
public static GuardedObject getGuardedObject(int id){
return boxes.remove(id);
}
/**
* 获取所有 GuardedObject实例 集合方法
*
* @return Set<Integer>
*/
public static Set<Integer> getIds() {
return boxes.keySet();
}
}
## 业务类
@Slf4j
class People extends Thread {
@Override
public void run() {
GuardedObject guardedObject = Futures.createGuardedObject();
log.debug("我是居民,我等待收 {}-号信",guardedObject.getId());
Object mail = guardedObject.get(5000);
log.debug("我是居民,我已收到 {}-号信, 内容:{}",guardedObject.getId(),mail);
}
}
@Slf4j
class Postman extends Thread {
private int id;
private String mail;
public Postman(int id, String mail) {
this.id = id;
this.mail = mail;
}
@Override
public void run() {
GuardedObject guardedObject = Futures.getGuardedObject(id);
log.debug("我是邮递员,我准备去送 {} 号信.",id);
guardedObject.complete(mail);
}
}
## 测试类
@Slf4j
public class Test {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 3; i++) {
People people = new People();
people.setName("居民");
people.start();
}
TimeUnit.SECONDS.sleep(1);
Set<Integer> ids = Futures.getIds();
for (Integer id : ids) {
Postman p = new Postman(id,"你好,我是"+id+"号信");
p.setName("邮递员");
p.start();
}
}
}
结果: