Class ClosableBlockingQueue<T>

  • Type Parameters:
    T - the type of elements
    All Implemented Interfaces:
    AutoCloseable

    @ThreadSafe
    public final class ClosableBlockingQueue<T>
    extends Object
    implements AutoCloseable
    A blocking queue implementation which is capable of closing.
    • Constructor Detail

      • ClosableBlockingQueue

        public ClosableBlockingQueue()
        Creates a closable blocking queue.
      • ClosableBlockingQueue

        public ClosableBlockingQueue​(int numElements)
        Creates a closable blocking queue.
        Parameters:
        numElements - the lower bound on initial capacity of the queue
    • Method Detail

      • put

        public void put​(T element)
        Adds an element.
        Parameters:
        element - the element to add
        Throws:
        IllegalStateException - if the input end of this queue has been closed
        NullPointerException - if element is null
      • close

        public void close()
        Mark the input end of this queue as closed.
        Specified by:
        close in interface AutoCloseable
      • closeExceptionally

        public void closeExceptionally​(Throwable throwableToSet)
        Mark the input end of this queue as closed.
        Parameters:
        throwableToSet - a throwable to set as the cause
      • take

        @Nullable
        public T take()
               throws InterruptedException
        Retrieves and removes the head of this queue, waiting if necessary.
        Returns:
        the head of this queue, or null if no elements are there and this queue has been closed
        Throws:
        InterruptedException - when interrupted while waiting
      • peek

        @Nullable
        public T peek()
               throws InterruptedException
        Retrieves, but does not removes, the head of this queue, waiting if necessary.
        Returns:
        the head of this queue, or null if no elements are there and this queue has been closed
        Throws:
        InterruptedException - when interrupted while waiting