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 booleanshutdown(java.util.concurrent.Executor executor)Attempts to shut down the givenExecutorif it is an instance ofExecutorService.static booleanshutdown(java.util.concurrent.ExecutorService executorService)Attempts to shut down the givenExecutorServicegracefully.static voidshutdownOnExit(java.util.concurrent.Executor one, java.util.concurrent.Executor... others)Registers a shutdown hook to gracefully shut down the givenExecutorinstances 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 givenExecutorinstances 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 firstExecutorto shut down on JVM exit; must not benullothers- additionalExecutorinstances to shut down; may be empty ornull
-
shutdown
public static boolean shutdown(java.util.concurrent.Executor executor)
Attempts to shut down the givenExecutorif it is an instance ofExecutorService.If the provided
Executoris 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: trueExecutor nonServiceExecutor = (runnable) -> new Thread(runnable).start(); boolean isShutdown = ExecutorUtils.shutdown(nonServiceExecutor); System.out.println("Executor shutdown: " + isShutdown); // Output: false- Parameters:
executor- theExecutorinstance to check and potentially shut down; may benull- Returns:
trueif the executor was anExecutorServiceand has been successfully shut down;falseotherwise
-
shutdown
public static boolean shutdown(java.util.concurrent.ExecutorService executorService)
Attempts to shut down the givenExecutorServicegracefully.This method checks if the provided
ExecutorServiceis 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: trueExecutorService alreadyShutdownExecutor = Executors.newSingleThreadExecutor(); alreadyShutdownExecutor.shutdown(); // manually shutting down boolean result = ExecutorUtils.shutdown(alreadyShutdownExecutor); System.out.println("Executor shutdown: " + result); // Output: false- Parameters:
executorService- theExecutorServiceinstance to shut down; may benull- Returns:
trueif the executor was actively running and has been successfully shut down;falseif it was already shutdown or null
-
-