Class ClosableBlockingQueue<T>

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

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Mark the input end of this queue as closed.
      void closeExceptionally​(java.lang.Throwable throwableToSet)
      Mark the input end of this queue as closed.
      T peek()
      Retrieves, but does not removes, the head of this queue, waiting if necessary.
      void put​(T element)
      Adds an element.
      T take()
      Retrieves and removes the head of this queue, waiting if necessary.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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:
        java.lang.IllegalStateException - if the input end of this queue has been closed
        java.lang.NullPointerException - if element is null
      • close

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

        public void closeExceptionally​(java.lang.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 java.lang.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:
        java.lang.InterruptedException - when interrupted while waiting
      • peek

        @Nullable
        public T peek()
               throws java.lang.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:
        java.lang.InterruptedException - when interrupted while waiting