Class AbstractTaskQueueExecutor

java.lang.Object
org.omegazero.common.event.AbstractTaskQueueExecutor
Direct Known Subclasses:
DeferringTaskQueueExecutor, DelegatingTaskQueueExecutor, SynchronousTaskQueueExecutor, TaskQueueExecutor

public abstract class AbstractTaskQueueExecutor extends Object
Used for queuing and running Tasks. The base class for a TaskQueueExecutor.

This class is thread-safe.

Since:
2.12.0
  • Constructor Details

    • AbstractTaskQueueExecutor

      public AbstractTaskQueueExecutor()
  • Method Details

    • queue

      public abstract boolean queue(Task task)
      Queues a task to be executed by any available worker thread.
      Parameters:
      task - The task to queue
      Returns:
      true if the task was successfully queued
      See Also:
    • unqueue

      public abstract boolean unqueue(Task task)
      Removes the given task from the queue.
      Parameters:
      task - The task to remove
      Returns:
      true if the task was queued previously and removed successfully
      See Also:
    • exit

      public abstract boolean exit(boolean blocking)
      Shuts this AbstractTaskQueueExecutor down by gracefully stopping the worker threads.
      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

      public abstract 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.

      If this handler is not set, the error will be printed to stderr.

      Parameters:
      errorHandler - The error handler, or null to remove an existing error handler
    • queue

      public boolean queue(Method method, Object callerInstance, int priority, Object... args)
      Creates a new ReflectTask instance and adds it to the event queue using queue(Task).
      Parameters:
      method - The task handler method
      callerInstance - The instance to call the method with. May be null if the method is static
      args - The arguments to pass to the task handler when this task is executed
      priority - The priority of this task. May be ignored if the backing queue does not support prioritization
      Returns:
      true if the task was successfully queued
      See Also:
    • queue

      public boolean queue(Runnable handler, int priority)
      Creates a new RunnableTask instance and adds it to the event queue using queue(Task).
      Parameters:
      handler - The task handler
      priority - The priority of this task. May be ignored if the backing queue does not support prioritization
      Returns:
      true if the task was successfully queued
      Since:
      2.9
      See Also:
    • queue

      public boolean queue(Consumer<Object[]> handler, int priority, Object... args)
      Creates a new LambdaTask instance and adds it to the event queue using queue(Task).
      Parameters:
      handler - The task handler
      args - The arguments to pass to the task handler when this task is executed
      priority - The priority of this task. May be ignored if the backing queue does not support prioritization
      Returns:
      true if the task was successfully queued
      See Also:
    • exit

      public void exit()
      Shuts this AbstractTaskQueueExecutor down by gracefully stopping the worker threads.

      Equivalent to a call to:

      
       exit(false)