Class TaskQueueExecutor
Tasks 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 classBuilder used to create aTaskQueueExecutor.classAn 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 TypeMethodDescriptionbooleanexit(boolean blocking) Shuts thisTaskQueueExecutordown by interrupting all idle worker threads, causing them to exit.static TaskQueueExecutor.BuilderCreates a newTaskQueueExecutor.Builderwith the given backing queue for theTaskQueueExecutor.static TaskQueueExecutor.Builderstatic TaskQueueExecutor.BuilderintReturns the configured maximum number of worker threads.intReturns the number of queued tasks.intReturns the current number of worker threads.booleanReturnstrueif the queue is empty.Creates a newTaskQueueExecutor.Handlefor use inqueue(Task, Handle).booleanQueues a task to be executed by any available worker thread.booleanqueue(Task task, TaskQueueExecutor.Handle handle) Queues a task to be executed by an available worker thread.voidsetErrorHandler(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.booleanRemoves 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:
queuein classAbstractTaskQueueExecutor- Parameters:
task- The task to queue- Returns:
trueif 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
falseif 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.Handlemay be given to control task execution. It is guaranteed that no more than one task with the sameHandlewill 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 andHandlewill be executed in the order they were added.- Parameters:
task- The task to queuehandle- An optionalHandleto control task execution- Returns:
trueif the task was successfully queued- Since:
- 2.9
- See Also:
-
unqueue
Description copied from class:AbstractTaskQueueExecutorRemoves the given task from the queue.- Specified by:
unqueuein classAbstractTaskQueueExecutor- Parameters:
task- The task to remove- Returns:
trueif the task was queued previously and removed successfully- See Also:
-
newHandle
Creates a newTaskQueueExecutor.Handlefor use inqueue(Task, Handle).If multiple tasks are queued with the same
Handleinstance, 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.Handles 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:
-
isQueueEmpty
public boolean isQueueEmpty()Returnstrueif the queue is empty.- Returns:
trueif 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 thisTaskQueueExecutordown 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:
exitin classAbstractTaskQueueExecutor- Parameters:
blocking-trueto wait for all worker threads to exit- Returns:
trueif 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:AbstractTaskQueueExecutorSets 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:
setErrorHandlerin classAbstractTaskQueueExecutor- Parameters:
errorHandler- The error handler, ornullto remove an existing error handler
-
from
Creates a newTaskQueueExecutor.Builderwith the given backing queue for theTaskQueueExecutor.- Parameters:
queue- The backing queue- Returns:
- The builder
- See Also:
-
fromPriority
Creates a newTaskQueueExecutor.Builderfor aTaskQueueExecutorbacked 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.Builderfor aTaskQueueExecutorbacked 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:
-