Package org.omegazero.common.event
Class DeferringTaskQueueExecutor
java.lang.Object
org.omegazero.common.event.AbstractTaskQueueExecutor
org.omegazero.common.event.DeferringTaskQueueExecutor
An
AbstractTaskQueueExecutor
deferring all queued tasks until another AbstractTaskQueueExecutor
implementation is available.
An object of this class should be used as a delegate for a DelegatingTaskQueueExecutor
.
This class is thread-safe.
- Since:
- 2.12.0
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newDeferringTaskQueueExecutor
with parameter maxDeferredTasks set to 1024.DeferringTaskQueueExecutor
(int maxDeferredTasks) Creates a newDeferringTaskQueueExecutor
. -
Method Summary
Modifier and TypeMethodDescriptionboolean
exit
(boolean blocking) Shuts thisAbstractTaskQueueExecutor
down by gracefully stopping the worker threads.boolean
Queues a task to be executed by any available worker thread.void
replaceFor
(DelegatingTaskQueueExecutor parent, AbstractTaskQueueExecutor newDelegate) Transfers all deferred tasks of thisDeferringTaskQueueExecutor
to a new delegate, then replaces the delegate of aDelegatingTaskQueueExecutor
, where the previous delegate was this object, with this new delegate.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.void
transferTasks
(AbstractTaskQueueExecutor destination) Transfers all deferred tasks of thisDeferringTaskQueueExecutor
to the givenAbstractTaskQueueExecutor
.boolean
Removes the given task from the queue.Methods inherited from class org.omegazero.common.event.AbstractTaskQueueExecutor
exit, queue, queue, queue
-
Constructor Details
-
DeferringTaskQueueExecutor
public DeferringTaskQueueExecutor()Creates a newDeferringTaskQueueExecutor
with parameter maxDeferredTasks set to 1024. -
DeferringTaskQueueExecutor
public DeferringTaskQueueExecutor(int maxDeferredTasks) Creates a newDeferringTaskQueueExecutor
.- Parameters:
maxDeferredTasks
- The maximum number of tasks to defer before rejectingqueue(Task)
calls
-
-
Method Details
-
transferTasks
Transfers all deferred tasks of thisDeferringTaskQueueExecutor
to the givenAbstractTaskQueueExecutor
. The defer queue is cleared and thisDeferringTaskQueueExecutor
can be used further.- Parameters:
destination
- TheAbstractTaskQueueExecutor
to transfer the deferred tasks to
-
replaceFor
Transfers all deferred tasks of thisDeferringTaskQueueExecutor
to a new delegate, then replaces the delegate of aDelegatingTaskQueueExecutor
, where the previous delegate was this object, with this new delegate.If an error handler was set for this
DeferringTaskQueueExecutor
, it is also set as the error handler for the new delegate.After a call to this method, this
DeferringTaskQueueExecutor
must no longer be used.- Parameters:
parent
- TheDelegatingTaskQueueExecutor
to set the new delegate fornewDelegate
- The new delegate- See Also:
-
queue
Description copied from class:AbstractTaskQueueExecutor
Queues a task to be executed by any available worker thread.- Specified by:
queue
in classAbstractTaskQueueExecutor
- Parameters:
task
- The task to queue- Returns:
true
if the task was successfully queued- 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:
-
exit
public boolean exit(boolean blocking) Description copied from class:AbstractTaskQueueExecutor
Shuts thisAbstractTaskQueueExecutor
down by gracefully stopping the worker threads.- 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
-
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
-