1. 定义
即 Guarded Suspension,用在一个线程等待另一个线程的执行结果
要点
- 有一个结果需要从一个线程传递到另一个线程,让他们关联同一个 GuardedObject
- 如果有结果不断从一个线程到另一个线程那么可以使用消息队列(见生产者/消费者)
- JDK 中,join 的实现、Future 的实现,采用的就是此模式 因为要等待另一方的结果,因此归类到同步模式
普通版
@Slf4j(topic = "Test22")
public class Test22 {
public static void main(String[] args) {
GuardedObject guardedObject = new GuardedObject();
//获取结果线程
new Thread(()->
{
log.info("等待结果");
Object o = guardedObject.get();
System.out.println(o.toString());
},"t1").start();
//产生结果线程
new Thread(()->
{
String str = "ceshi";
guardedObject.complete(str);
},"t2").start();
}
}
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();
}
}
带超时时间
class GuardedObject{
//结果
private Object response;
//获取结果
public Object get(long timeout){
synchronized (this){
//开始时间
long start = System.currentTimeMillis();
//消耗的时间
long passedTime = 0;
//没有结果,(为了避免虚假唤醒,需要用循环判断)
while(response == null){
long waiTime = timeout-passedTime;
if(waiTime <= 0){
break;
}
try {
this.wait(waiTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
passedTime = System.currentTimeMillis()-start;
}
return response;
}
}
//产生结果
public void complete(Object response){
synchronized (this){
this.response = response;
this.notifyAll();
}
}
}
多任务版 GuardedObject
- 图中 Futures 就好比居民楼一层的信箱(每个信箱有房间编号),左侧的 t0,t2,t4 就好比等待邮件的居民,右侧的
t1,t3,t5 就好比邮递员 - 如果需要在多个类之间使用 GuardedObject 对象,作为参数传递不是很方便,因此设计一个用来解耦的中间类,
这样不仅能够解耦【结果等待者】和【结果生产者】,还能够同时支持多个任务的管理
@Slf4j(topic = "Test22")
public class Test22 {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 3; i++) {
new People().start();
}
Thread.sleep(1);
for (Integer id : Mailboxes.getIds()) {
new Postman(id, "内容" + id).start();
}
}
}
@Slf4j(topic = "c.People")
class People extends Thread{
@Override
public void run() {
// 收信
GuardedObject guardedObject = Mailboxes.createGuardedObject();
log.debug("开始收信 id:{}", guardedObject.getId());
Object mail = guardedObject.get(5000);
log.debug("收到信 id:{}, 内容:{}", guardedObject.getId(), mail.toString());
}
}
@Slf4j(topic = "c.Postman")
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 = Mailboxes.getGuardedObject(id);
log.debug("送信 id:{}, 内容:{}", id, mail);
guardedObject.complete(mail);
}
}
//该类模拟 Futures 里面存放多个GuardedObject(信箱)
class Mailboxes{
//线程安全
private static Map<Integer, GuardedObject> boxes = new Hashtable<>();
private static int id = 1;
// 产生唯一 id,避免调用者随意定义
private static synchronized int generateId() {
return id++;
}
//创建 GuardedObject 对象,保存到map中
public static GuardedObject createGuardedObject() {
GuardedObject go = new GuardedObject(generateId());
boxes.put(go.getId(), go);
return go;
}
public static GuardedObject getGuardedObject(int id) {
return boxes.remove(id);
}
public static Set<Integer> getIds() {
return boxes.keySet();
}
}
class GuardedObject {
// 标识 Guarded Object
private int id;
public GuardedObject(int id) {
this.id = id;
}
public int getId() {
return id;
}
//结果
private Object response;
//获取结果
public Object get(long timeout) {
synchronized (this) {
//开始时间
long start = System.currentTimeMillis();
//消耗的时间
long passedTime = 0;
//没有结果,(为了避免虚假环境,需要用循环判断)
while (response == null) {
long waiTime = timeout - passedTime;
if (waiTime <= 0) {
break;
}
try {
this.wait(waiTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
passedTime = System.currentTimeMillis() - start;
}
return response;
}
}
//产生结果
public void complete(Object response) {
synchronized (this) {
this.response = response;
this.notifyAll();
}
}
}