Class ArrayBlockingQueueWithShutdown<E>

  • Type Parameters:
    E - the type of elements held in this collection
    All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.concurrent.BlockingQueue<E>, java.util.Queue<E>

    public class ArrayBlockingQueueWithShutdown<E>
    extends java.util.AbstractQueue<E>
    implements java.util.concurrent.BlockingQueue<E>
    Like ArrayBlockingQueue but with additional shutdown() and start() methods. Will throw InterruptedException if Queue has been shutdown on take() and poll(long, TimeUnit).

    Based on ArrayBlockingQueue of OpenJDK by Doug Lea (who released ArrayBlockingQueue as public domain).

    • Method Detail

      • shutdown

        public void shutdown()
        Shutdown the Queue. Will method currently waiting for a not full/empty condition will unblock (and usually throw a InterruptedException).
      • start

        public boolean start()
        Start the queue. Newly created instances will be started automatically, thus this only needs to be called after shutdown().
        Returns:
        true if the queues was shutdown before, false if not.
      • isShutdown

        public boolean isShutdown()
        Returns true if the queue is currently shut down.
        Returns:
        true if the queue is shut down.
      • poll

        public E poll()
        Specified by:
        poll in interface java.util.Queue<E>
      • peek

        public E peek()
        Specified by:
        peek in interface java.util.Queue<E>
      • offer

        public boolean offer​(E e)
        Specified by:
        offer in interface java.util.concurrent.BlockingQueue<E>
        Specified by:
        offer in interface java.util.Queue<E>
      • put

        public void put​(E e)
                 throws java.lang.InterruptedException
        Inserts the specified element into this queue, waiting if necessary for space to become available.

        This may throw an InterruptedException in two cases

        1. If the queue was shut down.
        2. If the thread was was interrupted.
        So you have to check which is the case, e.g. by calling isShutdown().
        Specified by:
        put in interface java.util.concurrent.BlockingQueue<E>
        Parameters:
        e - the element to add.
        Throws:
        java.lang.InterruptedException - if interrupted while waiting or if the queue was shut down.
      • putIfNotShutdown

        public boolean putIfNotShutdown​(E e)
                                 throws java.lang.InterruptedException
        Put if the queue has not been shutdown yet.
        Parameters:
        e - the element to put into the queue.
        Returns:
        true if the element has been put into the queue, false if the queue was shutdown.
        Throws:
        java.lang.InterruptedException - if the calling thread was interrupted.
        Since:
        4.4
      • putAll

        public void putAll​(java.util.Collection<? extends E> elements)
                    throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • offer

        public boolean offer​(E e,
                             long timeout,
                             java.util.concurrent.TimeUnit unit)
                      throws java.lang.InterruptedException
        Specified by:
        offer in interface java.util.concurrent.BlockingQueue<E>
        Throws:
        java.lang.InterruptedException
      • take

        public E take()
               throws java.lang.InterruptedException
        Specified by:
        take in interface java.util.concurrent.BlockingQueue<E>
        Throws:
        java.lang.InterruptedException
      • poll

        public E poll​(long timeout,
                      java.util.concurrent.TimeUnit unit)
               throws java.lang.InterruptedException
        Specified by:
        poll in interface java.util.concurrent.BlockingQueue<E>
        Throws:
        java.lang.InterruptedException
      • remainingCapacity

        public int remainingCapacity()
        Specified by:
        remainingCapacity in interface java.util.concurrent.BlockingQueue<E>
      • drainTo

        public int drainTo​(java.util.Collection<? super E> c)
        Specified by:
        drainTo in interface java.util.concurrent.BlockingQueue<E>
      • drainTo

        public int drainTo​(java.util.Collection<? super E> c,
                           int maxElements)
        Specified by:
        drainTo in interface java.util.concurrent.BlockingQueue<E>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in class java.util.AbstractCollection<E>
      • iterator

        public java.util.Iterator<Eiterator()
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in class java.util.AbstractCollection<E>