繁体   English   中英

Java BlockingQueue的大小= 1?

[英]Java BlockingQueue of Size=1?

本质上我想要的是大小为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);
}

您是说类似SynchronousQueue吗? 另一个有用的类是Exchanger

我在这里找到了一个名为“ ObjectLatch”的类的代码: http ://forums.sun.com/thread.jspa?threadID=5321141

package jfco.progs;

import java.util.concurrent.CountDownLatch;

/**
 * <H1>A Blocking Object Latch</H1> This class implements a blocking object
 * latch, that acts as a synchronizer between a producer of an object and it's
 * consumer(s).
 * <p>
 * An object is set with <code>set()</code>only ONCE. Further attempts to set
 * the object are just ignored.<br>
 * <p>
 * Consumers request the object with <code>get()</code>. If the object is not
 * already set, consumers are blocked waiting until the object is available or <br>
 * until an interrupt (InteruptedException) terminates the wait.
 * <p>
 * The latch can be tested for object availability with isAvailable(), which
 * answers true if the object has already been set. <br>
 */
public class ObjectLatch<R> {

    /** The object. */
    private R object = null;

    /** The latch counter created and set to 1. */
    private final CountDownLatch latch = new CountDownLatch(1);

    /**
     * Checks if the object is already available (has been already set).
     * 
     * @return true, if the object is already available (has been already set)
     */
    public boolean isAvailable() {
        return latch.getCount() == 0;
    }

    /**
     * Sets the object if it is not already set. Otherwise ignore this request.
     * 
     * @param object
     *            the object
     */
    public synchronized void set(R object) {
        if (!isAvailable()) {
            this.object = object;
            latch.countDown();
        }
    }

    /**
     * Get the object if it is already available (has already been set).
     * <p>
     * If it is not available, wait until it is or until an interrupt
     * (InterruptedException) terminates the wait.
     * 
     * @return the object if it is already available (has already been set)
     * 
     * @throws InterruptedException
     */
    public R get() throws InterruptedException {
        latch.await();
        synchronized (this) {
            return object;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM