Package io.microsphere.concurrent
Class ExecutorUtils
- java.lang.Object
-
- io.microsphere.concurrent.ExecutorUtils
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
shutdown(java.util.concurrent.Executor executor)
Attempts to shut down the givenExecutor
if it is an instance ofExecutorService
.static boolean
shutdown(java.util.concurrent.ExecutorService executorService)
Attempts to shut down the givenExecutorService
gracefully.static void
shutdownOnExit(java.util.concurrent.Executor one, java.util.concurrent.Executor... others)
Registers a shutdown hook to gracefully shut down the givenExecutor
instances when the JVM exits.
-
-
-
Method Detail
-
shutdownOnExit
public static void shutdownOnExit(java.util.concurrent.Executor one, java.util.concurrent.Executor... others)
Registers a shutdown hook to gracefully shut down the givenExecutor
instances when the JVM exits.This method adds a JVM shutdown hook using
ShutdownHookUtils.addShutdownHookCallback(Runnable)
, ensuring that all provided executors are shut down properly upon application exit.Example Usage
ExecutorService executor1 = Executors.newFixedThreadPool(2); ExecutorService executor2 = Executors.newSingleThreadExecutor(); ExecutorUtils.shutdownOnExit(executor1, executor2);
- Parameters:
one
- the firstExecutor
to shut down on JVM exit; must not benull
others
- additionalExecutor
instances to shut down; may be empty ornull
-
shutdown
public static boolean shutdown(java.util.concurrent.Executor executor)
Attempts to shut down the givenExecutor
if it is an instance ofExecutorService
.If the provided
Executor
is an instance ofExecutorService
, this method will delegate the shutdown process to theshutdown(ExecutorService)
method. Otherwise, no action is taken and the method returnsfalse
.Example Usage
ExecutorService executor = Executors.newFixedThreadPool(2); boolean isShutdown = ExecutorUtils.shutdown(executor); System.out.println("Executor shutdown: " + isShutdown); // Output: true
Executor nonServiceExecutor = (runnable) -> new Thread(runnable).start(); boolean isShutdown = ExecutorUtils.shutdown(nonServiceExecutor); System.out.println("Executor shutdown: " + isShutdown); // Output: false
- Parameters:
executor
- theExecutor
instance to check and potentially shut down; may benull
- Returns:
true
if the executor was anExecutorService
and has been successfully shut down;false
otherwise
-
shutdown
public static boolean shutdown(java.util.concurrent.ExecutorService executorService)
Attempts to shut down the givenExecutorService
gracefully.This method checks if the provided
ExecutorService
is not already shutdown. If it is still active, this method initiates an orderly shutdown by callingExecutorService.shutdown()
. If the executor is already shutdown or null, no action is taken and the method returnsfalse
.Example Usage
ExecutorService executor = Executors.newFixedThreadPool(2); boolean isShutdown = ExecutorUtils.shutdown(executor); System.out.println("Executor shutdown: " + isShutdown); // Output: true
ExecutorService alreadyShutdownExecutor = Executors.newSingleThreadExecutor(); alreadyShutdownExecutor.shutdown(); // manually shutting down boolean result = ExecutorUtils.shutdown(alreadyShutdownExecutor); System.out.println("Executor shutdown: " + result); // Output: false
- Parameters:
executorService
- theExecutorService
instance to shut down; may benull
- Returns:
true
if the executor was actively running and has been successfully shut down;false
if it was already shutdown or null
-
-