3

基本的に、サイズ = 1 の BlockingQueue が必要です。単純に待機し、オブジェクトがキューに入れられるまでブロックしてから取得する「リスナー」スレッドと、実際にオブジェクトをキューに入れる「プロデューサー」スレッドがあります。

いくつかの同期ブロックと BlockingQueue 実装でこれを実装できますが、それはやり過ぎのようです。私がやりたいことをするためのより良い、より簡単な方法はありますか?

インターフェースの例:

public interface Wait<T> {
  /**
   * If "put" has never been called on this object, then this method will
   * block and wait until it has. Once "put" has been called with some T, this
   * method will return that T immediately.
   */
  public T get() throws InterruptedException;

  /**
   * @param object The object to return to callers of get(). If called more
   * than once, will throw an {@link IllegalStateException}.
   */
  public void put(T object);
}
4

2 に答える 2

3

SynchronousQueueのようなものですか?別の便利なクラスはExchangerです

于 2008-10-18T06:22:21.140 に答える