Class ExecutorServiceBuilder


  • public final class ExecutorServiceBuilder
    extends java.lang.Object
    A builder for thread pools. This class is similar to Executors from JDK, but it gives better control over the parameters of the created thread pool and provides several extra features.
    • Automatic shutdown - the thread pool is properly shut down when the created ExecutorService instance becomes eligible for garbage collection.
    • Graceful shutdown - calling ExecutorService.shutdown() waits for tasks to finish.
    • Bound queue - the queue for submitted tasks has an upper limit (by default 100).
    • Core threads time out - the pool's core threads are allowed to time out and die by default.
    • Threads named by a caller - when creating a thread pool without specifying the name pattern for its threads the calling class name and line is used as the name pattern.
    • Context aware - the thread pool preserves ThreadContext.
    Since:
    16.1.18
    See Also:
    ThreadPoolExecutor, ThreadFactoryBuilder
    • Constructor Detail

      • ExecutorServiceBuilder

        public ExecutorServiceBuilder()
        Creates a new ExecutorService builder.
    • Method Detail

      • newSingleThreadExecutor

        public static java.util.concurrent.ExecutorService newSingleThreadExecutor​(java.lang.String threadNameFormat)
        Creates an ExecutorService that uses a single worker thread. The new thread pool has the default behavior as it is described for this class.
        Parameters:
        threadNameFormat - The String.format(String, Object...)-compatible format String that will be used for naming the worker thread.
        Returns:
        The newly created single-threaded ExecutorService.
      • newSingleThreadExecutor

        public static java.util.concurrent.ExecutorService newSingleThreadExecutor()
        Creates an ExecutorService that uses a single worker thread. The new thread pool has the default behavior as it is described for this class.
        Returns:
        The newly created single-threaded ExecutorService.
      • newFixedThreadPool

        public static java.util.concurrent.ExecutorService newFixedThreadPool​(int nThreads,
                                                                              java.lang.String threadNameFormat)
        Creates a thread pool that reuses a fixed number of threads. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. The new thread pool has the default behavior as it is described for this class.
        Parameters:
        nThreads - The maximum number of threads in the pool.
        threadNameFormat - The String.format(String, Object...)-compatible format String that will be used for naming the worker threads.
        Returns:
        The newly created thread pool.
      • newFixedThreadPool

        public static java.util.concurrent.ExecutorService newFixedThreadPool​(int nThreads)
        Creates a thread pool that reuses a fixed number of threads. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. The new thread pool has the default behavior as it is described for this class.
        Parameters:
        nThreads - The maximum number of threads in the pool.
        Returns:
        The newly created thread pool.
      • setUncaughtExceptionHandler

        public ExecutorServiceBuilder setUncaughtExceptionHandler​(java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler)
      • setThreadFactory

        public ExecutorServiceBuilder setThreadFactory​(java.util.concurrent.ThreadFactory backingThreadFactory)
      • setAllowCoreThreadTimeOut

        public ExecutorServiceBuilder setAllowCoreThreadTimeOut​(boolean allowCoreThreadTimeOut)
      • build

        public java.util.concurrent.ExecutorService build()