Class ExecutorManager

java.lang.Object
com.dianxin.tori.base.lifecycle.ExecutorManager

public final class ExecutorManager extends Object
Centralized manager handling core ExecutorService (Thread Pools) across the framework.

This manager segments execution environments into two distinct runtime channels:

  • IO Executor: Reserved for blocking operations waiting on external factors like networking or disk speeds (e.g., Database pools, HTTP Requests, File I/O).
  • CPU Executor: Tailored for compute-heavy, complex application logic execution that runs without thread blocking.

This utility class is entirely thread-safe and should be initialized exactly once during application bootstrap via initialize().

  • Method Details

    • initialize

      public static void initialize()
      Initializes default managed thread pool setups for the framework ecosystem.

      This execution block executes atomically once. Subsequent configuration attempts are completely ignored. Call this during the bootstrap sequence of your application.

    • initialize

      public static void initialize(ExecutorService io, ExecutorService cpu)
      Overloaded initialization method to pass custom external ExecutorService instances. Useful for specialized server configuration hooks or automated unit testing parameters.
      Parameters:
      io - the custom I/O bounded task executor service
      cpu - the custom CPU bounded task executor service
    • io

      public static ExecutorService io()
      Gets the ExecutorService designated for Input/Output (I/O) bound operations.

      This pool scales or holds a higher volume of open connections to seamlessly process multiple concurrently blocking network or thread-wait requests.

      Returns:
      the I/O specialized ExecutorService instance
      Throws:
      IllegalStateException - if initialize() hasn't been explicitly triggered beforehand
    • cpu

      public static ExecutorService cpu()
      Gets the ExecutorService designated for heavy data processing or CPU-bound tasks.

      Thread counts in this pool are strategically bounded by hardware resources (usually matching physical core capacities) to minimize context-switching overhead penalty.

      Returns:
      the calculation intensive ExecutorService instance
      Throws:
      IllegalStateException - if initialize() hasn't been explicitly triggered beforehand
    • shutdown

      public static void shutdown()
      Gracefully shuts down all managed running framework executor pools.

      Invoke this method during application/bot teardown lifecycles to guarantee proper cleanup of underlying thread resources.

    • runIoAsync

      public static <T> CompletableFuture<T> runIoAsync(@NotNull @NotNull Supplier<T> supplier)
      Executes an asynchronous background task on the managed io() executor pool.
      Type Parameters:
      T - the type of data returned by the execution block
      Parameters:
      supplier - the functional logic task wrapper returning a computation result of type T
      Returns:
      a CompletableFuture tracking the pending execution context status
    • runCpuAsync

      public static <T> CompletableFuture<T> runCpuAsync(@NotNull @NotNull Supplier<T> supplier)
      Executes an asynchronous background task on the managed cpu() executor pool.
      Type Parameters:
      T - the type of data returned by the execution block
      Parameters:
      supplier - the functional logic task wrapper returning a computation result of type T
      Returns:
      a CompletableFuture tracking the pending execution context status