Java BlockingQueue of Size=1?

2008-10-18T14:06:42

Essentially what I want is a BlockingQueue of size=1. I have a "listener" thread that simply waits, blocking until an object is put into the queue, and then retrieves it--and a "producer" thread that actually puts the object into the queue.

I can implement this with some synchronized blocks and a BlockingQueue implementation, but that seems like overkill. Is there a better, simpler way to do what I want?

Example interface:

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);
}

Copyright License:
Author:「Ross」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/214661/java-blockingqueue-of-size-1

About “Java BlockingQueue of Size=1?” questions

Essentially what I want is a BlockingQueue of size=1. I have a "listener" thread that simply waits, blocking until an object is put into the queue, and then retrieves it--and a "producer" thread that
I have a Java program that sent messages to an external Rest API. This API can handle a payload with a max size of 500KB only. My messages are stored in a BlockingQueue. I was wondering if there i...
I am currently reading about Java BlockingQueue and this example is given on many websites for a simple implementation of the BlockingQueue. The code is simple but I am a bit confused. For example ...
I have the problem regarding the implementation of One Publisher - Multiple Subscribers pattern. The Publisher uses the fixed-size buffer and queue the messages. The messages are send to all subscr...
I trying to write some blob from database to disk by passing an InputStream object to the queue. It creates the file with size 0. When I have tried write the InputStream to the disk without using a
If I write something like def l = [1, 2, 3] as Socket which is obviously nonsense, I get this: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[1, 2, 3]' with cl...
I have a potential race condition in my Java code that uses a BlockingQueue, and I'd like to know how to modify the code to avoid it: private static BlockingQueue&lt;FileToFTP&gt; ftpQueue = new
I recently saw the following implementation of enqueue for a BlockingQueue (source) public synchronized void enqueue(Object item) throws InterruptedException { while(this.queue.size() == this.l...
So I am using a fixed sized BlockingQueue [ArrayBlockingQueue] in a producer/consumer type application, but I want the user to be able to change the queue size on the fly. Problem is there is not a
The title of this question makes me doubt if this exist, but still: I'm interested in whether there is an implemented of Java's BlockingQueue, that is bounded by size, and never blocks, but rather

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.