Package com.im.commons.base
Class ExecutorServiceBuilder
- java.lang.Object
-
- com.im.commons.base.ExecutorServiceBuilder
-
public final class ExecutorServiceBuilder extends java.lang.Object
A builder for thread pools. This class is similar toExecutors
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
- Automatic shutdown - the thread pool is properly shut down when the created
-
-
Constructor Summary
Constructors Constructor Description ExecutorServiceBuilder()
Creates a newExecutorService
builder.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.concurrent.ExecutorService
build()
static java.util.concurrent.ExecutorService
newFixedThreadPool(int nThreads)
Creates a thread pool that reuses a fixed number of threads.static java.util.concurrent.ExecutorService
newFixedThreadPool(int nThreads, java.lang.String threadNameFormat)
Creates a thread pool that reuses a fixed number of threads.static java.util.concurrent.ExecutorService
newSingleThreadExecutor()
Creates anExecutorService
that uses a single worker thread.static java.util.concurrent.ExecutorService
newSingleThreadExecutor(java.lang.String threadNameFormat)
Creates anExecutorService
that uses a single worker thread.ExecutorServiceBuilder
setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
ExecutorServiceBuilder
setAutoShutdown(boolean autoShutdown)
ExecutorServiceBuilder
setContextAware(boolean contextAware)
ExecutorServiceBuilder
setCoreThreads(int coreThreads)
ExecutorServiceBuilder
setDaemon(boolean daemon)
ExecutorServiceBuilder
setKeepAliveTime(long keepAliveTime)
ExecutorServiceBuilder
setNameFormat(java.lang.String nameFormat)
ExecutorServiceBuilder
setPriority(int priority)
ExecutorServiceBuilder
setQueueCapacity(int queueCapacity)
ExecutorServiceBuilder
setShutdownGracefully(boolean shutdownGracefully)
ExecutorServiceBuilder
setThreadFactory(java.util.concurrent.ThreadFactory backingThreadFactory)
ExecutorServiceBuilder
setThreads(int nThreads)
ExecutorServiceBuilder
setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler)
-
-
-
Method Detail
-
newSingleThreadExecutor
public static java.util.concurrent.ExecutorService newSingleThreadExecutor(java.lang.String threadNameFormat)
Creates anExecutorService
that uses a single worker thread. The new thread pool has the default behavior as it is described for this class.- Parameters:
threadNameFormat
- TheString.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 anExecutorService
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 mostnThreads
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
- TheString.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 mostnThreads
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.
-
setNameFormat
public ExecutorServiceBuilder setNameFormat(java.lang.String nameFormat)
-
setDaemon
public ExecutorServiceBuilder setDaemon(boolean daemon)
-
setPriority
public ExecutorServiceBuilder setPriority(int priority)
-
setUncaughtExceptionHandler
public ExecutorServiceBuilder setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler)
-
setThreadFactory
public ExecutorServiceBuilder setThreadFactory(java.util.concurrent.ThreadFactory backingThreadFactory)
-
setThreads
public ExecutorServiceBuilder setThreads(int nThreads)
-
setCoreThreads
public ExecutorServiceBuilder setCoreThreads(int coreThreads)
-
setAllowCoreThreadTimeOut
public ExecutorServiceBuilder setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
-
setKeepAliveTime
public ExecutorServiceBuilder setKeepAliveTime(long keepAliveTime)
-
setAutoShutdown
public ExecutorServiceBuilder setAutoShutdown(boolean autoShutdown)
-
setShutdownGracefully
public ExecutorServiceBuilder setShutdownGracefully(boolean shutdownGracefully)
-
setContextAware
public ExecutorServiceBuilder setContextAware(boolean contextAware)
-
setQueueCapacity
public ExecutorServiceBuilder setQueueCapacity(int queueCapacity)
-
build
public java.util.concurrent.ExecutorService build()
-
-