TCP 経由でオブジェクトを受け取るワーカー スレッドへの参照のコレクションを保持するクラスがあります (クライアントごとに 1 つ)。
getMessage()
クラスで、いずれかのワーカー スレッドがメッセージを受け取り、それを返すまで待機するメソッドを作成しようとしています。
私が今得たのはポーリングシステムです:
public Object getMessage() {
while (true) {
for (Worker w : workers.values())
if (w.msgNumber() != 0)
return w.getLastMsg();
Thread.sleep(100);
}
}
動作しますが、あまりスケーラブルではないと思います。
wait(timeout)
各ワーカーで実行できることはわかっていますが、問題は同じままです。
複数のスレッドを待機するある種の待機/通知メカニズムはありますか?