Class ExecutorManager
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 Summary
Modifier and TypeMethodDescriptionstatic ExecutorServicecpu()Gets theExecutorServicedesignated for heavy data processing or CPU-bound tasks.static voidInitializes default managed thread pool setups for the framework ecosystem.static voidinitialize(ExecutorService io, ExecutorService cpu) Overloaded initialization method to pass custom externalExecutorServiceinstances.static ExecutorServiceio()Gets theExecutorServicedesignated for Input/Output (I/O) bound operations.static <T> CompletableFuture<T> runCpuAsync(@NotNull Supplier<T> supplier) Executes an asynchronous background task on the managedcpu()executor pool.static <T> CompletableFuture<T> runIoAsync(@NotNull Supplier<T> supplier) Executes an asynchronous background task on the managedio()executor pool.static voidshutdown()Gracefully shuts down all managed running framework executor pools.
-
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
Overloaded initialization method to pass custom externalExecutorServiceinstances. Useful for specialized server configuration hooks or automated unit testing parameters.- Parameters:
io- the custom I/O bounded task executor servicecpu- the custom CPU bounded task executor service
-
io
Gets theExecutorServicedesignated 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- ifinitialize()hasn't been explicitly triggered beforehand
-
cpu
Gets theExecutorServicedesignated 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- ifinitialize()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
Executes an asynchronous background task on the managedio()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
CompletableFuturetracking the pending execution context status
-
runCpuAsync
Executes an asynchronous background task on the managedcpu()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
CompletableFuturetracking the pending execution context status
-