1、定義
要點
-
有一個結果需要從一個執行緒傳遞到另一個執行緒,讓他們關聯同一個 GuardedObject
-
如果有結果不斷從一個執行緒到另一個執行緒那麼可以使用訊息佇列
-
JDK 中,join 的實現、Future 的實現,採用的就是此模式
-
因為要等待另一方的結果,因此歸類到同步模式
2、實現
class GuardedObject {
// 結果
private Object response;
private final Object lock = new Object();
// 獲取結果
public Object get() {
synchronized (lock) {
// 條件不滿足則等待
while (response == null) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return response;
}
}
// 產生結果
public void complete(Object response) {
synchronized (lock) {
// 條件滿足,通知等待執行緒
this.response = response;
lock.notifyAll();
}
}
}
3、應用
一個執行緒等待另一個執行緒的執行結果
public static void main(String[] args) {
GuardedObject guardedObject = new GuardedObject();
new Thread(() -> {
try {
// 子執行緒執行下載
List<String> response = download(); // 模擬下載操作
log.debug("download complete...");
guardedObject.complete(response);
} catch (IOException e) {
e.printStackTrace();
}
}).start();
log.debug("waiting...");
// 主執行緒阻塞等待
Object response = guardedObject.get();
log.debug("get response: [{}] lines", ((List<String>) response).size());
}
執行結果
08:42:18.568 [main] c.TestGuardedObject - waiting...
08:42:23.312 [Thread-0] c.TestGuardedObject - download complete...
08:42:23.312 [main] c.TestGuardedObject - get response: [3] lines
4、帶超時版 GuardedObject
如果要控制超時時間呢
class GuardedObjectV2 {
private Object response;
private final Object lock = new Object();
public Object get(long millis) {
synchronized (lock) {
// 1) 記錄最初時間
long begin = System.currentTimeMillis();
// 2) 已經經歷的時間
long timePassed = 0;
while (response == null) {
// 4) 假設 millis 是 1000,結果在 400 時喚醒了,那麼還有 600 要等
long waitTime = millis - timePassed;
log.debug("waitTime: {}", waitTime);
if (waitTime <= 0) {
log.debug("break...");
break;
}
try {
lock.wait(waitTime); // 注意這裡並不是 mills,防止虛假喚醒
} catch (InterruptedException e) {
e.printStackTrace();
}
// 3) 如果提前被喚醒,這時已經經歷的時間假設為 400
timePassed = System.currentTimeMillis() - begin;
log.debug("timePassed: {}, object is null {}",
timePassed, response == null);
}
return response;
}
}
public void complete(Object response) {
synchronized (lock) {
// 條件滿足,通知等待執行緒
this.response = response;
log.debug("notify...");
lock.notifyAll();
}
}
}
測試,沒有超時
public static void main(String[] args) {
GuardedObjectV2 v2 = new GuardedObjectV2();
new Thread(() -> {
sleep(1); // 睡眠1秒
v2.complete(null);
sleep(1);
v2.complete(Arrays.asList("a", "b", "c"));
}).start();
Object response = v2.get(2500);
if (response != null) {
log.debug("get response: [{}] lines", ((List<String>) response).size());
} else {
log.debug("can't get response");
}
}
輸出
08:49:39.917 [main] c.GuardedObjectV2 - waitTime: 2500
08:49:40.917 [Thread-0] c.GuardedObjectV2 - notify...
08:49:40.917 [main] c.GuardedObjectV2 - timePassed: 1003, object is null true
08:49:40.917 [main] c.GuardedObjectV2 - waitTime: 1497
08:49:41.918 [Thread-0] c.GuardedObjectV2 - notify...
08:49:41.918 [main] c.GuardedObjectV2 - timePassed: 2004, object is null false
08:49:41.918 [main] c.TestGuardedObjectV2 - get response: [3] lines
測試,超時
// 等待時間不足
List<String> lines = v2.get(1500);
輸出
08:47:54.963 [main] c.GuardedObjectV2 - waitTime: 1500
08:47:55.963 [Thread-0] c.GuardedObjectV2 - notify...
08:47:55.963 [main] c.GuardedObjectV2 - timePassed: 1002, object is null true
08:47:55.963 [main] c.GuardedObjectV2 - waitTime: 498
08:47:56.461 [main] c.GuardedObjectV2 - timePassed: 1500, object is null true
08:47:56.461 [main] c.GuardedObjectV2 - waitTime: 0
08:47:56.461 [main] c.GuardedObjectV2 - break...
08:47:56.461 [main] c.TestGuardedObjectV2 - can't get response
08:47:56.963 [Thread-0] c.GuardedObjectV2 - notify...
5、多工版 GuardedObject
圖中 Futures 就好比居民樓一層的信箱(每個信箱有房間編號),左側的 t0,t2,t4 就好比等待郵件的居民,右側的 t1,t3,t5 就好比郵遞員
如果需要在多個類之間使用 GuardedObject 物件,作為引數傳遞不是很方便,因此設計一個用來解耦的中間類,這樣不僅能夠解耦【結果等待者】和【結果生產者】,還能夠同時支援多個任務的管理
新增 id 用來標識 Guarded Object
class GuardedObject {
// 標識 Guarded Object
private int id;
public GuardedObject(int id) {
this.id = id;
}
public int getId() {
return id;
}
// 結果
private Object response;
// 獲取結果
// timeout 表示要等待多久 2000
public Object get(long timeout) {
synchronized (this) {
// 開始時間 15:00:00
long begin = System.currentTimeMillis();
// 經歷的時間
long passedTime = 0;
while (response == null) {
// 這一輪迴圈應該等待的時間
long waitTime = timeout - passedTime;
// 經歷的時間超過了最大等待時間時,退出迴圈
if (timeout - passedTime <= 0) {
break;
}
try {
this.wait(waitTime); // 虛假喚醒 15:00:01
} catch (InterruptedException e) {
e.printStackTrace();
}
// 求得經歷時間
passedTime = System.currentTimeMillis() - begin; // 15:00:02 1s
}
return response;
}
}
// 產生結果
public void complete(Object response) {
synchronized (this) {
// 給結果成員變數賦值
this.response = response;
this.notifyAll();
}
}
}
中間解耦類
class Mailboxes {
private static Map<Integer, GuardedObject> boxes = new Hashtable<>();
private static int id = 1;
// 產生唯一 id
private static synchronized int generateId() {
return id++;
}
public static GuardedObject getGuardedObject(int id) {
return boxes.remove(id); // 注意這裡的remove,防止堆溢位
}
public static GuardedObject createGuardedObject() {
GuardedObject go = new GuardedObject(generateId());
boxes.put(go.getId(), go);
return go;
}
public static Set<Integer> getIds() {
return boxes.keySet();
}
}
業務相關類
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);
}
}
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);
}
}
測試
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 3; i++) {
new People().start();
}
Sleeper.sleep(1);// 睡眠1秒
for (Integer id : Mailboxes.getIds()) {
new Postman(id, "內容" + id).start();
}
}
某次執行結果
10:35:05.689 c.People [Thread-1] - 開始收信 id:3
10:35:05.689 c.People [Thread-2] - 開始收信 id:1
10:35:05.689 c.People [Thread-0] - 開始收信 id:2
10:35:06.688 c.Postman [Thread-4] - 送信 id:2, 內容:內容2
10:35:06.688 c.Postman [Thread-5] - 送信 id:1, 內容:內容1
10:35:06.688 c.People [Thread-0] - 收到信 id:2, 內容:內容2
10:35:06.688 c.People [Thread-2] - 收到信 id:1, 內容:內容1
10:35:06.688 c.Postman [Thread-3] - 送信 id:3, 內容:內容3
10:35:06.689 c.People [Thread-1] - 收到信 id:3, 內容:內容3