別のスレッドの結果をスレッドに待機させる必要があることがよくあります。java.util.concurrent でこれをサポートする必要があるようですが、見つかりません。
Exchangerは私が話していることに非常に近いですが、双方向です。スレッド A がスレッド B で待機するだけで、両方が相互に待機することはありません。
はい、CountDownLatch または Semaphore または Thread.wait() を使用して、計算の結果を自分で管理できることはわかっていますが、便利なクラスがどこかに欠けているようです。
私は何が欠けていますか?
アップデート
// An Example which works using Exchanger
// but you would think there would be uni-directional solution
protected Exchanger<Integer> exchanger = new Exchanger<Integer>();
public void threadA() {
// perform some computations
int result = ...;
exchanger.exchange(result);
}
public void threadB() {
// retrieve the result of threadA
int resultOfA = exchanger.exchange(null);
}