Class ExecutorServiceHelpers


  • public final class ExecutorServiceHelpers
    extends java.lang.Object
    Helper methods for ExecutorService.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void execute​(RunnableWithException task, java.util.function.Consumer<java.lang.Throwable> exceptionHandler, java.lang.Runnable runFinally, java.util.concurrent.Executor executor)
      Executes the given task on the given Executor.
      static java.util.concurrent.ThreadPoolExecutor getShrinkingExecutor​(int maxThreadCount, int threadTimeout, java.lang.String poolName)
      Operates like Executors.cachedThreadPool but with a custom thread timeout and pool name.
      static ExecutorServiceHelpers.Snapshot getSnapshot​(java.util.concurrent.ExecutorService service)
      Gets a snapshot of the given ExecutorService.
      static java.util.concurrent.ThreadFactory getThreadFactory​(java.lang.String groupName)
      Creates and returns a thread factory that will create threads with the given name prefix.
      static java.util.concurrent.ThreadFactory getThreadFactory​(java.lang.String groupName, int priority)
      Creates and returns a thread factory that will create threads with the given name prefix and thread priority.
      static java.util.concurrent.ScheduledExecutorService newScheduledThreadPool​(int size, java.lang.String poolName)
      Creates a new ScheduledExecutorService that will use daemon threads with appropriate names the threads.
      static java.util.concurrent.ScheduledExecutorService newScheduledThreadPool​(int size, java.lang.String poolName, int threadPriority)
      Creates a new ScheduledExecutorService that will use daemon threads with specified priority and names.
      static void shutdown​(java.time.Duration timeout, java.util.concurrent.ExecutorService... pools)
      Shuts down the given ExecutorServices in two phases: 1.
      static void shutdown​(java.util.concurrent.ExecutorService... pools)
      Shuts down the given ExecutorServices in two phases, using a timeout of 5 seconds: 1.
      • Methods inherited from class java.lang.Object

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

      • ExecutorServiceHelpers

        public ExecutorServiceHelpers()
    • Method Detail

      • getThreadFactory

        public static java.util.concurrent.ThreadFactory getThreadFactory​(java.lang.String groupName)
        Creates and returns a thread factory that will create threads with the given name prefix.
        Parameters:
        groupName - the name of the threads
        Returns:
        a thread factory
      • getThreadFactory

        public static java.util.concurrent.ThreadFactory getThreadFactory​(java.lang.String groupName,
                                                                          int priority)
        Creates and returns a thread factory that will create threads with the given name prefix and thread priority.
        Parameters:
        groupName - the name of the threads
        priority - the priority to be assigned to the thread.
        Returns:
        a thread factory
      • newScheduledThreadPool

        public static java.util.concurrent.ScheduledExecutorService newScheduledThreadPool​(int size,
                                                                                           java.lang.String poolName)
        Creates a new ScheduledExecutorService that will use daemon threads with appropriate names the threads.
        Parameters:
        size - The number of threads in the threadpool
        poolName - The name of the pool (this will be printed in logs)
        Returns:
        A new executor service.
      • newScheduledThreadPool

        public static java.util.concurrent.ScheduledExecutorService newScheduledThreadPool​(int size,
                                                                                           java.lang.String poolName,
                                                                                           int threadPriority)
        Creates a new ScheduledExecutorService that will use daemon threads with specified priority and names.
        Parameters:
        size - The number of threads in the threadpool
        poolName - The name of the pool (this will be printed in logs)
        threadPriority - The priority to be assigned to the threads
        Returns:
        A new executor service.
      • getSnapshot

        public static ExecutorServiceHelpers.Snapshot getSnapshot​(java.util.concurrent.ExecutorService service)
        Gets a snapshot of the given ExecutorService.
        Parameters:
        service - The ExecutorService to request a snapshot on.
        Returns:
        A Snapshot of the given ExecutorService, or null if not supported.
      • getShrinkingExecutor

        public static java.util.concurrent.ThreadPoolExecutor getShrinkingExecutor​(int maxThreadCount,
                                                                                   int threadTimeout,
                                                                                   java.lang.String poolName)
        Operates like Executors.cachedThreadPool but with a custom thread timeout and pool name.
        Parameters:
        maxThreadCount - The maximum number of threads to allow in the pool.
        threadTimeout - the number of milliseconds that a thread should sit idle before shutting down.
        poolName - The name of the threadpool.
        Returns:
        A new threadPool
      • execute

        public static void execute​(RunnableWithException task,
                                   java.util.function.Consumer<java.lang.Throwable> exceptionHandler,
                                   java.lang.Runnable runFinally,
                                   java.util.concurrent.Executor executor)
        Executes the given task on the given Executor.
        Parameters:
        task - The RunnableWithException to execute.
        exceptionHandler - A Consumer that will be invoked in case the task threw an Exception. This is not invoked if the executor could not execute the given task.
        runFinally - A Runnable that is guaranteed to be invoked at the end of this execution. If the executor did accept the task, it will be invoked after the task is complete (or ended in failure). If the executor did not accept the task, it will be executed when this method returns.
        executor - An Executor to execute the task on.
      • shutdown

        public static void shutdown​(java.util.concurrent.ExecutorService... pools)
        Shuts down the given ExecutorServices in two phases, using a timeout of 5 seconds: 1. Prevents new tasks from being submitted. 2. Awaits for currently running tasks to terminate. If they don't terminate within the given timeout, they will be forcibly cancelled.
        Parameters:
        pools - The ExecutorServices to shut down.
      • shutdown

        public static void shutdown​(java.time.Duration timeout,
                                    java.util.concurrent.ExecutorService... pools)
        Shuts down the given ExecutorServices in two phases: 1. Prevents new tasks from being submitted. 2. Awaits for currently running tasks to terminate. If they don't terminate within the given timeout, they will be forcibly cancelled. This is implemented as per the guidelines in the ExecutorService Javadoc: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
        Parameters:
        timeout - Grace period that will be given to tasks to complete.
        pools - The ExecutorServices to shut down.