Class ExecutorTools


  • public final class ExecutorTools
    extends Object
    Executor-related utility methods.
    • Method Detail

      • awaitTermination

        public static void awaitTermination​(ExecutorService executorService)
        Waits indefinitely and uninterruptibly (!) until the given executor service is terminated.
        Parameters:
        executorService - The executor service of which to await its termination. If this is null no waiting will take place.
      • awaitTermination

        public static void awaitTermination​(ExecutorService executorService,
                                            long interruptAfter)
        Waits indefinitely and uninterruptibly (!) until the given executor service is terminated. If there is an interruptAfter time (positive number), ExecutorService.shutdownNow() will be called after this time and waiting will continue.
        Parameters:
        executorService - The executor service of which to await its termination. If this is null no waiting will take place.
        interruptAfter - The time in milliseconds when to interrupt the designated executor service in case it has not terminated then.
      • createExecutor

        public static ExecutorService createExecutor​(int poolSize,
                                                     String name,
                                                     Logger logger,
                                                     boolean timeout,
                                                     int queueLimitPoolStatistics,
                                                     Level minLogLevelPoolStatistics)
        Creates a new executor service with the designated core pool size using the designated name and logger to create a Adept2ThreadFactory for creating threads.
        Parameters:
        poolSize - The amount of threads to keep in the pool. No additional threads will be created.
        name - The name that will be used to create an Adept2ThreadFactory for createExecutor(int, Adept2ThreadFactory, boolean)
        logger - The logger that will be used to create an Adept2ThreadFactory for createExecutor(int, Adept2ThreadFactory, boolean).
        timeout - Whether unused core threads should time out. If set to false, the core threads will be prestarted.
        queueLimitPoolStatistics - The amount of tasks in the queue before warnings will be logged.
        minLogLevelPoolStatistics - The log level which need to be reached for logging pool statistics.
        Returns:
        A new executor service using a LinkedBlockingQueue.
      • createExecutor

        public static ExecutorService createExecutor​(int poolSize,
                                                     Adept2ThreadFactory factory,
                                                     boolean timeout)
        Creates a new executor service with the designated core pool size using the designated name and logger to create a Adept2ThreadFactory for creating threads.
        Parameters:
        poolSize - The amount of threads to keep in the pool. No additional threads will be created.
        factory - The factory to use when the executor creates a new thread and for handling (logging) exceptions occurred while executing.
        timeout - Whether unused core threads should time out. If set to false, the core threads will be prestarted.
        Returns:
        A new executor service using a LinkedBlockingQueue.
      • createExecutor

        public static ExecutorService createExecutor​(int poolSize,
                                                     Adept2ThreadFactory factory,
                                                     boolean timeout,
                                                     int queueLimitPoolStatistics)
        Creates a new executor service with the designated core pool size using the designated name and logger to create a Adept2ThreadFactory for creating threads.
        Parameters:
        poolSize - The amount of threads to keep in the pool. No additional threads will be created.
        factory - The factory to use when the executor creates a new thread and for handling (logging) exceptions occurred while executing.
        timeout - Whether unused core threads should time out. If set to false, the core threads will be prestarted.
        queueLimitPoolStatistics - The amount of tasks in the queue before warnings will be logged.
        Returns:
        A new executor service using a LinkedBlockingQueue.
      • createExecutor

        public static ExecutorService createExecutor​(int poolSize,
                                                     Adept2ThreadFactory factory,
                                                     boolean timeout,
                                                     int queueLimitPoolStatistics,
                                                     Level minLogLevelPoolStatistics)
        Creates a new executor service with the designated core pool size using the designated name and logger to create a Adept2ThreadFactory for creating threads.
        Parameters:
        poolSize - The amount of threads to keep in the pool. No additional threads will be created.
        factory - The factory to use when the executor creates a new thread and for handling (logging) exceptions occurred while executing.
        timeout - Whether unused core threads should time out. If set to false, the core threads will be prestarted.
        queueLimitPoolStatistics - The amount of tasks in the queue before warnings will be logged.
        minLogLevelPoolStatistics - The log level which need to be reached for logging pool statistics for.
        Returns:
        A new executor service using a LinkedBlockingQueue.
      • createCurrentExecutor

        public static ExecutorService createCurrentExecutor()
        Creates a new executor service executing commands by the submitting thread.
        Returns:
        A new executor service that will execute commands directly by the submitting thread.
      • createBoundedExecutor

        public static ExecutorService createBoundedExecutor​(int maxPoolSize,
                                                            String name,
                                                            Logger logger)
        Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool, until the maximum pool size is reached. After that the runnables will be executed in the caller thread. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources.
        Parameters:
        maxPoolSize - The maximum number of threads to allow in the pool.
        name - The name that will be used to create an Adept2ThreadFactory for the executor.
        logger - The logger that will be used to create an Adept2ThreadFactory for the executor.
        Returns:
        A new executor service using a SynchronousQueue and the ThreadPoolExecutor.CallerRunsPolicy as rejection execution handler.
      • createCachedExecutor

        public static ExecutorService createCachedExecutor​(int corePoolSize,
                                                           int maxPoolSize,
                                                           String name,
                                                           Logger logger)
        Creates a new CachedThreadPoolExecutor with the given initial parameters and default rejected execution handler. The cached thread pool executor increases the corePoolSize to the maxPoolSize, if the work load exceeds the amount of core threads. If the work load declines, the corePoolSize will be reduced to the original value.
        Parameters:
        corePoolSize - The number of threads to keep in the pool, even if they are idle.
        maxPoolSize - The maximum number of threads to allow in the pool.
        name - The name that will be used to create an Adept2ThreadFactory for the executor.
        logger - The logger that will be used to create an Adept2ThreadFactory for the executor.
        Returns:
        A new executor service using a LinkedBlockingQueue which will start until maxPoolSize threads, if the work load exceeds the corePoolSize.
      • createCachedExecutor

        public static ExecutorService createCachedExecutor​(int corePoolSize,
                                                           int maxPoolSize,
                                                           String name,
                                                           int queueLimitPoolStatistics,
                                                           Level minLogLevelPoolStatistics,
                                                           Logger logger)
        Creates a new CachedThreadPoolExecutor with the given initial parameters and default rejected execution handler. The cached thread pool executor increases the corePoolSize to the maxPoolSize, if the work load exceeds the amount of core threads. If the work load declines, the corePoolSize will be reduced to the original value.
        Parameters:
        corePoolSize - The number of threads to keep in the pool, even if they are idle.
        maxPoolSize - The maximum number of threads to allow in the pool.
        name - The name that will be used to create an Adept2ThreadFactory for the executor.
        queueLimitPoolStatistics - The amount of tasks in the queue before warnings will be logged.
        minLogLevelPoolStatistics - The log level which need to be reached for logging pool statistics.
        logger - The logger that will be used to create an Adept2ThreadFactory for the executor.
        Returns:
        A new executor service using a LinkedBlockingQueue which will start until maxPoolSize threads, if the work load exceeds the corePoolSize.
      • createCachedExecutor

        public static ExecutorService createCachedExecutor​(int corePoolSize,
                                                           int maxPoolSize,
                                                           Adept2ThreadFactory factory)
        Creates a new CachedThreadPoolExecutor with the given initial parameters and default rejected execution handler. The cached thread pool executor increases the corePoolSize to the maxPoolSize, if the work load exceeds the amount of core threads. If the work load declines, the corePoolSize will be reduced to the original value.
        Parameters:
        corePoolSize - The number of threads to keep in the pool, even if they are idle.
        maxPoolSize - The maximum number of threads to allow in the pool.
        factory - The factory to use when the executor creates a new thread.
        Returns:
        A new executor service using a LinkedBlockingQueue which will start until maxPoolSize threads, if the work load exceeds the corePoolSize.
      • createCachedExecutor

        @Deprecated(since="14.4.0",
                    forRemoval=true)
        public static ExecutorService createCachedExecutor​(int corePoolSize,
                                                           int maxPoolSize,
                                                           boolean prestart,
                                                           Adept2ThreadFactory factory)
        Deprecated, for removal: This API element is subject to removal in a future version.
        Creates a new CachedThreadPoolExecutor with the given initial parameters and default rejected execution handler. The cached thread pool executor increases the corePoolSize to the maxPoolSize, if the work load exceeds the amount of core threads. If the work load declines, the corePoolSize will be reduced to the original value.
        Parameters:
        corePoolSize - The number of threads to keep in the pool, even if they are idle.
        maxPoolSize - The maximum number of threads to allow in the pool.
        prestart - Whether all core threads should be prestarted.
        factory - The factory to use when the executor creates a new thread.
        Returns:
        A new executor service using a LinkedBlockingQueue which will start until maxPoolSize threads, if the work load exceeds the corePoolSize.
      • createScheduledExecutor

        public static LoggingScheduledThreadPoolExecutor createScheduledExecutor​(int poolSize,
                                                                                 Adept2ThreadFactory factory,
                                                                                 boolean continueRegularAfterException)
        Creates a new scheduled executor service with the designated minimal pool size using the designated factory for creating threads. This method configures the service so that delayed and periodic tasks waiting for their next execution are terminated as soon as ExecutorService#shutdown() is called. Otherwise these tasks wait for their next scheduled execution before they terminate.
        Additionally, regular scheduled tasks (ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit) and ScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, TimeUnit) may be set to log runtime exceptions and virtual machine errors and continue regular execution instead of failing and stopping.

        Important: A scheduled executor service uses nanoseconds for scheduling. This may differ from milliseconds. Therefore when using milliseconds for checking conditions in a scheduled runnable, use some tolerance (at least about 50 ms) since the scheduled point in time may not have been reached in milliseconds (but in nanoseconds). See David Holmes' Weblog.

        Parameters:
        poolSize - The number of threads to keep in the pool. No additional threads will be created.
        factory - The factory to use when the executor creates a new thread and for handling (logging) exceptions occurred while executing.
        continueRegularAfterException - Whether regular executions should continue even in case of runtime exceptions and virtual machine errors. These exceptions will be logged.
        Returns:
        A new scheduled executor service cancelling delayed and periodic tasks when shutdown, optionally continuing regular tasks in case of unexpected exceptions.