Class CancellableQueue<T extends @NonNull java.lang.Object>


  • @ThreadSafe
    public class CancellableQueue<T extends @NonNull java.lang.Object>
    extends java.lang.Object
    A simplified ThreadSafe blocking queue that can be cancelled freeing any blocked Threads and preventing future Threads from blocking.

    The queue is able to be reset and re-used.

    • Constructor Summary

      Constructors 
      Constructor Description
      CancellableQueue​(int capacity)
      Creates a ThreadSafe blocking queue with a maximum capacity.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cancel​(java.lang.Exception exception)
      Causes any pending and future put(T) and take() invocations to throw an exception.
      void put​(T t)
      Adds an element to this queue.
      void reset()
      Enables the queue to be re-used after it has been cancelled.
      T take()
      Takes an element from this queue.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CancellableQueue

        public CancellableQueue​(int capacity)
        Creates a ThreadSafe blocking queue with a maximum capacity.
    • Method Detail

      • put

        public void put​(T t)
                 throws java.lang.Exception,
                        java.lang.InterruptedException
        Adds an element to this queue. Will block until the queue is not full or is cancelled.
        Throws:
        java.lang.InterruptedException - if this thread was interrupted waiting to put the element. The caller must invoke cancel(java.lang.Exception) if the interrupt is unrecoverable.
        java.lang.Exception - if the queue is cancelled.
      • take

        public T take()
               throws java.lang.Exception,
                      java.lang.InterruptedException
        Takes an element from this queue. Will block until the queue is not full or is cancelled.
        Throws:
        java.lang.InterruptedException - if this thread was interrupted waiting for an element. The caller must invoke cancel(java.lang.Exception) if the interrupt is unrecoverable.
        java.lang.Exception - if the queue is cancelled.
      • cancel

        public void cancel​(java.lang.Exception exception)
        Causes any pending and future put(T) and take() invocations to throw an exception.

        The first call to cancel(java.lang.Exception) sets the exception that will be thrown. Resetting the queue clears the exception.

      • reset

        public void reset()
        Enables the queue to be re-used after it has been cancelled.