Class ExecutorUtils

  • All Implemented Interfaces:
    Utils

    public abstract class ExecutorUtils
    extends java.lang.Object
    implements Utils
    Executor Utilities class
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    Executor, ExecutorService, Executors
    • 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 given Executor if it is an instance of ExecutorService.
      static boolean shutdown​(java.util.concurrent.ExecutorService executorService)
      Attempts to shut down the given ExecutorService gracefully.
      static void shutdownOnExit​(java.util.concurrent.Executor one, java.util.concurrent.Executor... others)
      Registers a shutdown hook to gracefully shut down the given Executor instances when the JVM exits.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 given Executor 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 first Executor to shut down on JVM exit; must not be null
        others - additional Executor instances to shut down; may be empty or null
      • shutdown

        public static boolean shutdown​(java.util.concurrent.Executor executor)
        Attempts to shut down the given Executor if it is an instance of ExecutorService.

        If the provided Executor is an instance of ExecutorService, this method will delegate the shutdown process to the shutdown(ExecutorService) method. Otherwise, no action is taken and the method returns false.

        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 - the Executor instance to check and potentially shut down; may be null
        Returns:
        true if the executor was an ExecutorService and has been successfully shut down; false otherwise
      • shutdown

        public static boolean shutdown​(java.util.concurrent.ExecutorService executorService)
        Attempts to shut down the given ExecutorService gracefully.

        This method checks if the provided ExecutorService is not already shutdown. If it is still active, this method initiates an orderly shutdown by calling ExecutorService.shutdown(). If the executor is already shutdown or null, no action is taken and the method returns false.

        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 - the ExecutorService instance to shut down; may be null
        Returns:
        true if the executor was actively running and has been successfully shut down; false if it was already shutdown or null