I'm looking for a queue that would be the asynchronous (non-blocking) equivalent of java.util.concurrent.BlockingQueue
. Its interface would include:
public interface AsynchronousBlockingQueue<E> {
// - if the queue is empty, return a new CompletableFuture,
// that will be completed next time `add` is called
// - if the queue is not empty, return a completed CompletableFuture,
containing the first element of the list
public CompletableFuture<E> poll();
// if polling is in progress, complete the ongoing polling CompletableFuture.
// otherwise, add the element to the queue
public synchronized void add(E element);
}
If that matters, there should be just one poller thread, and polling should be done sequentially (poll
will not be called when polling is already in progress).
I expected it to already exist in the JVM, but I couldn't find it, and of course I'd rather use something from the JVM than write it myself.
Another constraint, I'm stuck with Java 8 (even though I'm definitely interested in knowing what exists in more recent versions).