Class TaskQueue
- All Implemented Interfaces:
Serializable, Iterable<Runnable>, Collection<Runnable>, BlockingQueue<Runnable>, Queue<Runnable>, RetryableQueue<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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final StringManagerThe string manager for this package. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new TaskQueue with default capacity.TaskQueue(int capacity) Constructs a new TaskQueue with the given capacity.TaskQueue(Collection<? extends Runnable> c) Constructs a new TaskQueue containing the elements in the given collection. -
Method Summary
Modifier and TypeMethodDescriptionbooleanUsed to add a task to the queue if the task has been rejected by the Executor.booleanRemoves and returns the head of this queue, waiting up to the specified wait time if necessary for an element to become available.voidSets the parent thread pool executor for this queue.take()Methods inherited from class LinkedBlockingQueue
clear, contains, drainTo, drainTo, forEach, iterator, offer, peek, poll, put, remainingCapacity, remove, removeAll, removeIf, retainAll, size, spliterator, toArray, toArray, toStringMethods inherited from class AbstractQueue
add, addAll, element, removeMethods inherited from class AbstractCollection
containsAll, isEmptyMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface BlockingQueue
add, contains, drainTo, drainTo, offer, put, remainingCapacity, removeMethods inherited from interface Collection
addAll, clear, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray, toArray
-
Field Details
-
sm
The string manager for this package.
-
-
Constructor Details
-
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
Constructs a new TaskQueue containing the elements in the given collection.- Parameters:
c- the collection of initial elements
-
-
Method Details
-
setParent
Sets the parent thread pool executor for this queue.- Parameters:
tp- the parent executor
-
force
Description copied from interface:RetryableQueueUsed to add a task to the queue if the task has been rejected by the Executor.- Specified by:
forcein interfaceRetryableQueue<Runnable>- Parameters:
o- The task to add to the queue- Returns:
trueif the task was added to the queue, otherwisefalse
-
offer
- Specified by:
offerin interfaceBlockingQueue<Runnable>- Specified by:
offerin interfaceQueue<Runnable>- Overrides:
offerin classLinkedBlockingQueue<Runnable>
-
poll
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:
pollin interfaceBlockingQueue<Runnable>- Overrides:
pollin classLinkedBlockingQueue<Runnable>- Parameters:
timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- the head of this queue, or
nullif the specified waiting time elapses before an element becomes available - Throws:
InterruptedException- if interrupted while waitingStopPooledThreadException- if the current thread is due to be stopped by the parent executor
-
take
- Specified by:
takein interfaceBlockingQueue<Runnable>- Overrides:
takein classLinkedBlockingQueue<Runnable>- Throws:
InterruptedException
-