Class SerializingExecutor

All Implemented Interfaces:
Runnable, Executor, INotifier, INotifier.INotifier2, IDeactivateable, ILifecycle, ILifecycle.DeferrableActivation

public class SerializingExecutor extends Lifecycle implements Executor, Runnable
An executor that runs submitted tasks one at a time, in the order in which they are queued, using a delegate executor. The delegate remains externally owned. Before the first activation, execute(Runnable) and all submit methods retain tasks until activation. After deactivation, execute(Runnable) continues to retain tasks until a later activation, while submit rejects new tasks. Tasks that were queued when deactivation begins are discarded, and their submitted futures are cancelled.
Since:
3.9
Author:
Eike Stepper
  • Constructor Details

    • SerializingExecutor

      public SerializingExecutor(Executor delegate)
    • SerializingExecutor

      public SerializingExecutor()
  • Method Details

    • getDelegate

      public final Executor getDelegate()
    • setDelegate

      public final void setDelegate(Executor delegate)
    • execute

      public final void execute(Runnable command)
      Executes the given command in queue order. Before the first activation, and after deactivation, the command is retained until a later activation. A command queued when deactivation begins is discarded.
      Specified by:
      execute in interface Executor
      Parameters:
      command - the command to execute
      Throws:
      NullPointerException - if the command is null
    • submit

      public final Future<?> submit(Runnable task)
      Queues a task for serial execution and returns a future that completes when the task completes. Before the first activation, the task is retained until activation. After deactivation, this method rejects the task. If scheduling the worker is rejected, this method throws the rejection and cancels any futures that were already accepted but cannot be scheduled as a result.
      Parameters:
      task - the task to execute
      Returns:
      the future representing the task
      Throws:
      RejectedExecutionException - if this executor is deactivated or the delegate rejects the worker submission
      NullPointerException - if the task is null
      Since:
      3.31
    • submit

      public final <T> Future<T> submit(Runnable task, T result)
      Queues a task for serial execution and returns a future that yields the given result after successful completion. Before the first activation, the task is retained until activation. After deactivation, this method rejects the task. Cancellation follows the standard Future contract.
      Type Parameters:
      T - the result type
      Parameters:
      task - the task to execute
      result - the result to return after successful execution
      Returns:
      the future representing the task
      Throws:
      RejectedExecutionException - if this executor is deactivated or the delegate rejects the worker submission
      NullPointerException - if the task is null
      Since:
      3.31
    • submit

      public final <T> Future<T> submit(Callable<T> task)
      Queues a callable for serial execution and returns a future for its result or failure. Before the first activation, the task is retained until activation. After deactivation, this method rejects the task.
      Type Parameters:
      T - the result type
      Parameters:
      task - the callable to execute
      Returns:
      the future representing the task
      Throws:
      RejectedExecutionException - if this executor is deactivated or the delegate rejects the worker submission
      NullPointerException - if the task is null
      Since:
      3.31
    • run

      public final void run()
      Specified by:
      run in interface Runnable
    • handleFailedTask

      protected void handleFailedTask(Runnable task, Throwable failure)
    • doActivate

      protected void doActivate() throws Exception
      Overrides:
      doActivate in class Lifecycle
      Throws:
      Exception
    • doAfterActivate

      protected void doAfterActivate() throws Exception
      Overrides:
      doAfterActivate in class Lifecycle
      Throws:
      Exception
    • doDeactivate

      protected void doDeactivate() throws Exception
      Overrides:
      doDeactivate in class Lifecycle
      Throws:
      Exception