Class ThreadPoolUtils


  • public final class ThreadPoolUtils
    extends java.lang.Object
    Utility methods for working with thread pools ExecutorService.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static long DEFAULT_SHUTDOWN_AWAIT_TERMINATION
      Default thread pool shutdown timeout used to await graceful shutdown.
    • Constructor Summary

      Constructors 
      Constructor Description
      ThreadPoolUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean awaitTermination​(java.util.concurrent.ExecutorService executorService, long shutdownAwaitTermination)
      Awaits the termination of the thread pool.
      static void shutdown​(java.util.concurrent.ExecutorService executorService)
      Shutdown the given executor service only (ie not graceful shutdown).
      static void shutdownGraceful​(java.util.concurrent.ExecutorService executorService)
      Shutdown the given executor service graceful at first, and then aggressively if the await termination timeout was hit.
      static void shutdownGraceful​(java.util.concurrent.ExecutorService executorService, long shutdownAwaitTermination)
      Shutdown the given executor service graceful at first, and then aggressively if the await termination timeout was hit.
      static java.util.List<java.lang.Runnable> shutdownNow​(java.util.concurrent.ExecutorService executorService)
      Shutdown now the given executor service aggressively.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_SHUTDOWN_AWAIT_TERMINATION

        public static final long DEFAULT_SHUTDOWN_AWAIT_TERMINATION
        Default thread pool shutdown timeout used to await graceful shutdown.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ThreadPoolUtils

        public ThreadPoolUtils()
    • Method Detail

      • shutdown

        public static void shutdown​(java.util.concurrent.ExecutorService executorService)
        Shutdown the given executor service only (ie not graceful shutdown).
        Parameters:
        executorService - The ExecutorService that is being shutdown.
        See Also:
        ExecutorService.shutdown()
      • shutdownNow

        public static java.util.List<java.lang.Runnable> shutdownNow​(java.util.concurrent.ExecutorService executorService)
        Shutdown now the given executor service aggressively.
        Parameters:
        executorService - the executor service to shutdown now
        Returns:
        list of tasks that never commenced execution
        See Also:
        ExecutorService.shutdownNow()
      • shutdownGraceful

        public static void shutdownGraceful​(java.util.concurrent.ExecutorService executorService,
                                            long shutdownAwaitTermination)
        Shutdown the given executor service graceful at first, and then aggressively if the await termination timeout was hit.

        Will try to perform an orderly shutdown by giving the running threads time to complete tasks, before going more aggressively by doing a shutdownNow(java.util.concurrent.ExecutorService) which forces a shutdown. The parameter shutdownAwaitTermination is used as timeout value waiting for orderly shutdown to complete normally, before going aggressively.

        Parameters:
        executorService - the executor service to shutdown
        shutdownAwaitTermination - timeout in millis to wait for orderly shutdown
      • awaitTermination

        public static boolean awaitTermination​(java.util.concurrent.ExecutorService executorService,
                                               long shutdownAwaitTermination)
                                        throws java.lang.InterruptedException
        Awaits the termination of the thread pool.

        This implementation will log every 2nd second at INFO level that we are waiting, so the end user can see we are not hanging in case it takes longer time to terminate the pool.

        Parameters:
        executorService - the thread pool
        shutdownAwaitTermination - time in millis to use as timeout
        Returns:
        true if the pool is terminated, or false if we timed out
        Throws:
        java.lang.InterruptedException - is thrown if we are interrupted during the waiting