Class TaskQueue

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<java.lang.Runnable>, java.util.Collection<java.lang.Runnable>, java.util.concurrent.BlockingQueue<java.lang.Runnable>, java.util.Queue<java.lang.Runnable>, RetryableQueue<java.lang.Runnable>

    public class TaskQueue
    extends java.util.concurrent.LinkedBlockingQueue<java.lang.Runnable>
    implements RetryableQueue<java.lang.Runnable>
    A task queue specifically designed to run with a thread pool executor. The task queue is optimised to properly utilize threads within a thread pool executor. A normal queue causes the executor to queue tasks first and only create new threads once the queue is full. This queue instead refuses to queue a task while all existing threads are busy and the pool size is below its maximum, so the executor creates a new thread for the task instead. It also supports forcing items onto the queue via force(Runnable).
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static StringManager sm
      The string manager for this package.
    • Constructor Summary

      Constructors 
      Constructor Description
      TaskQueue()
      Constructs a new TaskQueue with default capacity.
      TaskQueue​(int capacity)
      Constructs a new TaskQueue with the given capacity.
      TaskQueue​(java.util.Collection<? extends java.lang.Runnable> c)
      Constructs a new TaskQueue containing the elements in the given collection.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean force​(java.lang.Runnable o)
      Used to add a task to the queue if the task has been rejected by the Executor.
      boolean offer​(java.lang.Runnable o)  
      java.lang.Runnable poll​(long timeout, java.util.concurrent.TimeUnit unit)
      Removes and returns the head of this queue, waiting up to the specified wait time if necessary for an element to become available.
      void setParent​(ThreadPoolExecutor tp)
      Sets the parent thread pool executor for this queue.
      java.lang.Runnable take()  
      • Methods inherited from class java.util.concurrent.LinkedBlockingQueue

        clear, contains, drainTo, drainTo, forEach, iterator, offer, peek, poll, put, remainingCapacity, remove, removeAll, removeIf, retainAll, size, spliterator, toArray, toArray, toString
      • Methods inherited from class java.util.AbstractQueue

        add, addAll, element, remove
      • Methods inherited from class java.util.AbstractCollection

        containsAll, isEmpty
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.concurrent.BlockingQueue

        add, contains, drainTo, drainTo, offer, put, remainingCapacity, remove
      • Methods inherited from interface java.util.Collection

        addAll, clear, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.Queue

        element, peek, poll, remove
    • Field Detail

      • sm

        protected static final StringManager sm
        The string manager for this package.
    • Constructor Detail

      • TaskQueue

        public TaskQueue()
        Constructs a new TaskQueue with default capacity.
      • TaskQueue

        public TaskQueue​(int capacity)
        Constructs a new TaskQueue with the given capacity.
        Parameters:
        capacity - the queue capacity
      • TaskQueue

        public TaskQueue​(java.util.Collection<? extends java.lang.Runnable> c)
        Constructs a new TaskQueue containing the elements in the given collection.
        Parameters:
        c - the collection of initial elements
    • Method Detail

      • setParent

        public void setParent​(ThreadPoolExecutor tp)
        Sets the parent thread pool executor for this queue.
        Parameters:
        tp - the parent executor
      • force

        public boolean force​(java.lang.Runnable o)
        Description copied from interface: RetryableQueue
        Used to add a task to the queue if the task has been rejected by the Executor.
        Specified by:
        force in interface RetryableQueue<java.lang.Runnable>
        Parameters:
        o - The task to add to the queue
        Returns:
        true if the task was added to the queue, otherwise false
      • offer

        public boolean offer​(java.lang.Runnable o)
        Specified by:
        offer in interface java.util.concurrent.BlockingQueue<java.lang.Runnable>
        Specified by:
        offer in interface java.util.Queue<java.lang.Runnable>
        Overrides:
        offer in class java.util.concurrent.LinkedBlockingQueue<java.lang.Runnable>
      • poll

        public java.lang.Runnable poll​(long timeout,
                                       java.util.concurrent.TimeUnit unit)
                                throws java.lang.InterruptedException
        Removes and returns the head of this queue, waiting up to the specified wait time if necessary for an element to become available.
        Specified by:
        poll in interface java.util.concurrent.BlockingQueue<java.lang.Runnable>
        Overrides:
        poll in class java.util.concurrent.LinkedBlockingQueue<java.lang.Runnable>
        Parameters:
        timeout - the maximum time to wait
        unit - the time unit of the timeout argument
        Returns:
        the head of this queue, or null if the specified waiting time elapses before an element becomes available
        Throws:
        java.lang.InterruptedException - if interrupted while waiting
        StopPooledThreadException - if the current thread is due to be stopped by the parent executor
      • take

        public java.lang.Runnable take()
                                throws java.lang.InterruptedException
        Specified by:
        take in interface java.util.concurrent.BlockingQueue<java.lang.Runnable>
        Overrides:
        take in class java.util.concurrent.LinkedBlockingQueue<java.lang.Runnable>
        Throws:
        java.lang.InterruptedException