// multiple server instances (replicas), coordinated using MsgCoordinationService
public class Server {
ConcurrentHashMap<TxID,Future<Msg>> local_registry = new ...
MsgCoordinationService coordination_service = new ..
...
// Socket instance to communicate with a client...
public void accept(Socket s) {
new Thread(new Worker(s)).start();
}
// propose msg to coordination service, register demand to respond to client in local registry
public Future<Msg> register(Msg m) {
FutureMsg f = new MsgFuture(); // Future handle w. reference to an empty Msg object [= response]
TxID uniqueID = coordination_service.propose(s); // transaction ID
local_registry.add(uniqueID, f);
return f;
}
// called by coordination service, guaranteeing a global order on msg deliveries
public synchronized void deliver(TxID id, Msg m) {
... process Msg object [request]
... if local_registry.contains(id), 'compile' response
(using the Msg object from FutureMsg f, f.get() - f.isDone() when a certain Msg flag has been set)
___ now:
... notify waiting 'Worker' threads to check whether their 'Future' object isDone()
}
private class Worker implements Runnable {
...
public void run() {
...
Future<Msg> f = Server.this.register(request); // obtained through Socket s
while(!f.isDone())
wait();
response = f.get();
...
}
}
}
我正在實施複製服務[多個服務器,客戶端通信w。一個服務器實例,創建/更新/刪除操作將通過協調服務的方式進行分配,以確保全球消息傳送順序]。分層死鎖(有依賴關係),...或者:如何獲得我的「等待」,「通知」和設計是否正確?
一旦客戶端建立到服務器實例的新連接,所有通信都將通過專用的Worker
實例[本地處理讀取請求,並使用Server.this.register(...)
廣播C/U/D操作]。
register
本身基本上記錄了未來本地處理/回覆的請求 - 並將Msg
對象轉發給協調服務。
服務通過deliver
...重新提供Msg
對象,並且在處理封裝的任務之後,應該通知最初接收到客戶端請求的實例,以交付相應的響應。
由於某些原因,我的設計似乎被打破... - 沒有synchronized(this)
[在Worker#run()
],wait()
不會等待;與synchronized(this)
,notifyAll()
Server#deliver(...)
將不會釋放「阻止」Worker
實例上的鎖定。 (a):瞭解wait/notify/notifyAll
的基本知識或(b):改進我的設計...或(c):(()):(a)和(b)。