1、定义
保护性暂停,即Guarded Suspension,用在一个线程等待另一个线程的执行结果
- 有一个结果需要从一个线程传递到另一个线程,让他们关联同一个GuardedObject
- 如果有结果不断从一个线程到另一个线程那么可以使用消息队列(生产者/消费者)
- jdk中,Join的实现,Future的实现,采用的就是此模式
- 因为要等待另一方的结果,因此归类到同步模式
2、实现
class GuardedObject {
//结果
private Object response;
//获取结果
public Object get() {
synchronized (this) {
//没有结果
while(response == null) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return response;
}
}
//产生结果
public void complete(Object response) {
synchronized (this) {
//给结果成员变量赋值
this.response = response;
this.notifyAll();
}
}
}
3、增加超时
class GuardedObject {
//结果
private Object response;
//获取结果
public Object get(long timeout) {
synchronized (this) {
//开始时间
long begin = System.currentTimeMillis();
//经历时间
long passedTime = 0;
while(response == null) {
//经历的时间超过了最大等待时间时,退出循环
if(passedTime >= timeout) {
break;
}
try {
this.wait(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
//求得经历时间
passedTime = System.currntTimeMillis() - begin;
}
return response;
}
}
//产生结果
public void complete(Object response) {
synchronized (this) {
//给结果成员变量赋值
this.response = response;
this.notifyAll();
}
}
}
4、join原理
底层也是wait,notify实现
5、解耦等待和生产
图中Futures 就好比居民楼一层的信箱(每个信箱有房间编号),左侧的t0,t2,t4就好比等待邮件的居民,右侧的t1, t3,t5就好比邮递员
如果需要在多个类之间使用GuardedObject对象,作为参数传递不是很方便,因此设计一个用来解耦的中间类,这样不仅能够解耦【结果等待者】和【结果生产者】,还能够同时支持多个任务的管理
class People extends Thread {
public void run() {
//收信
GuardedObject guardedObject = Mailboxes.createGuardedObject();
Object mail = guardedObject.get(5000);
}
}
class Postman extends Thread {
private int id;
private String mail;
//送信
public void run() {
GuardedObject guardedObject = Mailboxes.getGuardedObject(id);
guardedObject.complete(mail)
}
}
class Mailboxes {
private Map<Integer,GuardedObject> boxes = new Hashtable<>();
private static int id = 1;
//产生唯一id
private static synchronized int generateId() {
return id++;
}
public static GuardedObject getGuardObject(int id) {
return boxes.get(id);
}
public static GuardedObject createGuaredeObject(){
GuardedObject go = new GuardedObject(generateId());
boxes.put(go.getId(),go);
reeturn go;
}
public static Set<Integer> getIds() {
return boxes.keySet();
}
}