Class DirectExecutorService

java.lang.Object
java.util.concurrent.AbstractExecutorService
io.fluxcapacitor.common.DirectExecutorService
All Implemented Interfaces:
AutoCloseable, Executor, ExecutorService

public class DirectExecutorService extends AbstractExecutorService
A simple implementation of AbstractExecutorService that executes tasks directly in the calling thread without using any worker threads.

This class facilitates immediate execution of tasks provided to it. It does not queue tasks or execute them asynchronously. Instead, tasks are executed sequentially on the calling thread. This behavior makes it particularly useful in certain testing scenarios or for lightweight use cases where threading control is not necessary.

This class maintains an internal state to track whether the executor has been shut down, whether it is terminated, and which tasks are currently running. Once shut down, no new tasks can be executed.

Features: - Tasks are executed directly in the calling thread, without any delay or queueing. - Keeps track of running tasks and prevents task execution after the executor has been shut down. - Supports shutting down and forced shutdown via shutdown() and shutdownNow(). - Provides termination and shutdown status via isShutdown() and isTerminated(). - Allows the caller to wait for termination using awaitTermination(long, TimeUnit).

Thread Safety: Internally uses thread-safe mechanisms such as AtomicBoolean and CopyOnWriteArrayList to handle state tracking and concurrent invocations.

  • Constructor Details

    • DirectExecutorService

      public DirectExecutorService()
  • Method Details

    • newInstance

      public static ExecutorService newInstance()
    • execute

      public void execute(@NonNull @NonNull Runnable command)
    • shutdown

      public void shutdown()
    • isShutdown

      public boolean isShutdown()
    • shutdownNow

      @NotNull public List<Runnable> shutdownNow()
    • isTerminated

      public boolean isTerminated()
    • awaitTermination

      public boolean awaitTermination(long timeout, @NotNull TimeUnit unit) throws InterruptedException
      Throws:
      InterruptedException