Class TaskQueueExecutor
Task
s concurrently, backed by a Queue
, which need not be thread-safe. Up to a predefined number of threads can be used for executing tasks.
These parameters may be configured in the constructor or using the TaskQueueExecutor.Builder
.
Tasks are queued using the queue(Task)
methods. Upon queuing a task, any applicable idle worker thread may pick up the task and execute it.
Generally, if no TaskQueueExecutor.Handle
is used, no guarantee is made about the order in which tasks with the same priority will be executed, as this is also dependent on the backing
queue. However, if the backing queue guarantees insertion order for equal priority tasks, and a single worker thread is used, these tasks will be processed in the order in which
they were queued.
This class is thread-safe.
- Since:
- 2.6
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Builder used to create aTaskQueueExecutor
.class
An opaque class used for task execution control. -
Constructor Summary
ConstructorsConstructorDescriptionTaskQueueExecutor
(Queue<Task> queue, int maxWorkerThreadCount, boolean daemon, String threadName) Creates a newTaskQueueExecutor
. -
Method Summary
Modifier and TypeMethodDescriptionboolean
exit
(boolean blocking) Shuts thisTaskQueueExecutor
down by interrupting all idle worker threads, causing them to exit.static TaskQueueExecutor.Builder
Creates a newTaskQueueExecutor.Builder
with the given backing queue for theTaskQueueExecutor
.static TaskQueueExecutor.Builder
static TaskQueueExecutor.Builder
int
Returns the configured maximum number of worker threads.int
Returns the number of queued tasks.int
Returns the current number of worker threads.boolean
Returnstrue
if the queue is empty.Creates a newTaskQueueExecutor.Handle
for use inqueue(Task, Handle)
.boolean
Queues a task to be executed by any available worker thread.boolean
queue
(Task task, TaskQueueExecutor.Handle handle) Queues a task to be executed by an available worker thread.void
setErrorHandler
(Consumer<Throwable> errorHandler) Sets the error handler that will be called when an error occurs while executing a task in any of the worker threads.boolean
Removes the given task from the queue.Methods inherited from class org.omegazero.common.event.AbstractTaskQueueExecutor
exit, queue, queue, queue
-
Constructor Details
-
TaskQueueExecutor
public TaskQueueExecutor(Queue<Task> queue, int maxWorkerThreadCount, boolean daemon, String threadName) Creates a newTaskQueueExecutor
.- Parameters:
queue
- The backing queuemaxWorkerThreadCount
- The maximum number of worker threads alloweddaemon
- Whether worker threads should be daemon threadsthreadName
- The thread name prefix of the worker threads- See Also:
-
-
Method Details
-
queue
Queues a task to be executed by any available worker thread.Equivalent to a call to:
queue
(task, null)- Specified by:
queue
in classAbstractTaskQueueExecutor
- Parameters:
task
- The task to queue- Returns:
true
if the task was successfully queued- See Also:
-
queue
Queues a task to be executed by an available worker thread. If the backing queue supports prioritization, tasks with the lowest priority value will be executed first.This call may fail and return
false
if the task could not be added because the queue has reached its maximum size.If no worker thread is available or there is a backlog of tasks, and the number of worker threads is below the maximum, a new worker thread will be created.
An optional
TaskQueueExecutor.Handle
may be given to control task execution. It is guaranteed that no more than one task with the sameHandle
will be executed concurrently (however, note that it is still undefined by which thread each task is executed). Additionally, if the backing queue guarantees insertion order for tasks with the same priority, it is also guaranteed that all tasks with the same priority andHandle
will be executed in the order they were added.- Parameters:
task
- The task to queuehandle
- An optionalHandle
to control task execution- Returns:
true
if the task was successfully queued- Since:
- 2.9
- See Also:
-
unqueue
Description copied from class:AbstractTaskQueueExecutor
Removes the given task from the queue.- Specified by:
unqueue
in classAbstractTaskQueueExecutor
- Parameters:
task
- The task to remove- Returns:
true
if the task was queued previously and removed successfully- See Also:
-
newHandle
Creates a newTaskQueueExecutor.Handle
for use inqueue(Task, Handle)
.If multiple tasks are queued with the same
Handle
instance, it is guaranteed that no more than one task will be executed concurrently (however, note that it is still undefined by which thread each task is executed). Additionally, if the backing queue guarantees insertion order for tasks with the same priority, it is also guaranteed that all tasks with the same priority will be executed in the order they were added.Handle
s need not be explicitly closed and can simply be discarded (left unreferenced) if no longer used.- Returns:
- The new
Handle
- Since:
- 2.9
- See Also:
-
randomHandleThreads
-
isQueueEmpty
public boolean isQueueEmpty()Returnstrue
if the queue is empty.- Returns:
true
if the queue is empty
-
getQueuedTaskCount
public int getQueuedTaskCount()Returns the number of queued tasks.- Returns:
- Number of queued tasks
-
exit
public boolean exit(boolean blocking) Shuts thisTaskQueueExecutor
down by interrupting all idle worker threads, causing them to exit. Worker threads that are currently running a task are not interrupted, but will exit after finishing the task.If blocking is
true
, the calling thread will be blocked until all worker threads have exited. If the calling thread is interrupted while waiting, this method returnsfalse
.- Specified by:
exit
in classAbstractTaskQueueExecutor
- Parameters:
blocking
-true
to wait for all worker threads to exit- Returns:
true
if the calling thread was interrupted while waiting for the worker threads to exit
-
getMaxWorkerThreadCount
public int getMaxWorkerThreadCount()Returns the configured maximum number of worker threads.- Returns:
- The maximum number of worker threads
-
getWorkerThreadCount
public int getWorkerThreadCount()Returns the current number of worker threads.- Returns:
- The number of active worker threads
- See Also:
-
setErrorHandler
Description copied from class:AbstractTaskQueueExecutor
Sets the error handler that will be called when an error occurs while executing a task in any of the worker threads.If this handler is not set, the error will be printed to
stderr
.- Specified by:
setErrorHandler
in classAbstractTaskQueueExecutor
- Parameters:
errorHandler
- The error handler, ornull
to remove an existing error handler
-
from
Creates a newTaskQueueExecutor.Builder
with the given backing queue for theTaskQueueExecutor
.- Parameters:
queue
- The backing queue- Returns:
- The builder
- See Also:
-
fromPriority
Creates a newTaskQueueExecutor.Builder
for aTaskQueueExecutor
backed by aPriorityQueue
. This queue supports prioritization, but makes no guarantee about the order at which tasks with the same priority are executed.- Returns:
- The builder
- See Also:
-
fromSequential
Creates a newTaskQueueExecutor.Builder
for aTaskQueueExecutor
backed by aArrayDeque
. This queue does not support prioritization, but has insertion order, meaning tasks will generally be executed in the order they were queued.- Returns:
- The builder
- See Also:
-