Class SpecialExecutors
ExecutorService
instances with specific configurations.- Author:
- Thomas Pantelis
-
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutorService
newBlockingBoundedCachedThreadPool
(int maximumPoolSize, int maximumQueueSize, String threadPrefix, Class<?> loggerIdentity) Creates an ExecutorService similar tonewBoundedCachedThreadPool(int, int, java.lang.String, java.lang.Class<?>)
except that it handles rejected tasks by running them in the same thread as the caller.static ExecutorService
newBlockingBoundedFastThreadPool
(int maximumPoolSize, int maximumQueueSize, String threadPrefix, Class<?> loggerIdentity) Creates an ExecutorService similar tonewBoundedFastThreadPool(int, int, java.lang.String, java.lang.Class<?>)
except that it handles rejected tasks by running them in the same thread as the caller.static ExecutorService
newBoundedCachedThreadPool
(int maximumPoolSize, int maximumQueueSize, String threadPrefix, Class<?> loggerIdentity) Creates an ExecutorService with a specified bounded queue capacity that favors reusing previously constructed threads, when they are available, over creating new threads.static ExecutorService
newBoundedFastThreadPool
(int maximumPoolSize, int maximumQueueSize, String threadPrefix, Class<?> loggerIdentity) Creates an ExecutorService with a specified bounded queue capacity that favors creating new threads over queuing, as the former is faster, so threads will only be reused when the thread limit is exceeded and tasks are queued.static ExecutorService
newBoundedSingleThreadExecutor
(int maximumQueueSize, String threadPrefix, Class<?> loggerIdentity) Creates an ExecutorService that uses a single worker thread operating off a bounded queue with the specified capacity.
-
Method Details
-
newBoundedFastThreadPool
public static ExecutorService newBoundedFastThreadPool(int maximumPoolSize, int maximumQueueSize, String threadPrefix, Class<?> loggerIdentity) Creates an ExecutorService with a specified bounded queue capacity that favors creating new threads over queuing, as the former is faster, so threads will only be reused when the thread limit is exceeded and tasks are queued. If the maximum queue capacity is reached, subsequent tasks will be rejected.For example, if the maximum number of threads is 100 and 100 short-lived tasks are submitted within say 10 seconds, then 100 threads will be created and used - previously constructed idle threads will not be reused. This provides the fastest execution of the 100 tasks at the expense of memory and thread resource overhead. Therefore it is advisable to specify a relatively small thread limit (probably no more than 50).
Threads that have not been used for 15 seconds are terminated and removed from the pool. Thus, a pool that remains idle for long enough will not consume any resources.
If you need an executor with less memory and thread resource overhead where slower execution time is acceptable, consider using
newBoundedCachedThreadPool(int, int, java.lang.String, java.lang.Class<?>)
.- Parameters:
maximumPoolSize
- the maximum number of threads to allow in the pool. Threads will terminate after being idle for 15 seconds.maximumQueueSize
- the capacity of the queue.threadPrefix
- the name prefix for threads created by this executor.loggerIdentity
- the class to use as logger name for logging uncaught exceptions from the threads.- Returns:
- a new ExecutorService with the specified configuration.
-
newBlockingBoundedFastThreadPool
public static ExecutorService newBlockingBoundedFastThreadPool(int maximumPoolSize, int maximumQueueSize, String threadPrefix, Class<?> loggerIdentity) Creates an ExecutorService similar tonewBoundedFastThreadPool(int, int, java.lang.String, java.lang.Class<?>)
except that it handles rejected tasks by running them in the same thread as the caller. Therefore if the queue is full, the caller submitting the task will be blocked until the task completes. In this manner, tasks are never rejected.- Parameters:
maximumPoolSize
- the maximum number of threads to allow in the pool. Threads will terminate after being idle for 15 seconds.maximumQueueSize
- the capacity of the queue.threadPrefix
- the name prefix for threads created by this executor.loggerIdentity
- the class to use as logger name for logging uncaught exceptions from the threads.- Returns:
- a new ExecutorService with the specified configuration.
-
newBoundedCachedThreadPool
public static ExecutorService newBoundedCachedThreadPool(int maximumPoolSize, int maximumQueueSize, String threadPrefix, Class<?> loggerIdentity) Creates an ExecutorService with a specified bounded queue capacity that favors reusing previously constructed threads, when they are available, over creating new threads. When a task is submitted, if no existing thread is available, a new thread will be created and added to the pool. If there is an existing idle thread available, the task will be handed to that thread to execute. If the specified maximum thread limit is reached, subsequent tasks will be queued and will execute as threads become available. If the maximum queue capacity is reached, subsequent tasks will be rejected.Threads that have not been used for sixty seconds are terminated and removed from the pool. Thus, a pool that remains idle for long enough will not consume any resources.
By reusing threads when possible, this executor optimizes for reduced memory and thread resource overhead at the expense of execution time.
If you need an executor with faster execution time where increased memory and thread resource overhead is acceptable, consider using
newBoundedFastThreadPool(int, int, java.lang.String, java.lang.Class<?>)
.- Parameters:
maximumPoolSize
- the maximum number of threads to allow in the pool. Threads will terminate after being idle for 60 seconds.maximumQueueSize
- the capacity of the queue.threadPrefix
- the name prefix for threads created by this executor.- Returns:
- a new ExecutorService with the specified configuration.
-
newBlockingBoundedCachedThreadPool
public static ExecutorService newBlockingBoundedCachedThreadPool(int maximumPoolSize, int maximumQueueSize, String threadPrefix, Class<?> loggerIdentity) Creates an ExecutorService similar tonewBoundedCachedThreadPool(int, int, java.lang.String, java.lang.Class<?>)
except that it handles rejected tasks by running them in the same thread as the caller. Therefore if the queue is full, the caller submitting the task will be blocked until the task completes. In this manner, tasks are never rejected.- Parameters:
maximumPoolSize
- the maximum number of threads to allow in the pool. Threads will terminate after being idle for 60 seconds.maximumQueueSize
- the capacity of the queue.threadPrefix
- the name prefix for threads created by this executor.- Returns:
- a new ExecutorService with the specified configuration.
-
newBoundedSingleThreadExecutor
public static ExecutorService newBoundedSingleThreadExecutor(int maximumQueueSize, String threadPrefix, Class<?> loggerIdentity) Creates an ExecutorService that uses a single worker thread operating off a bounded queue with the specified capacity. Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. If the maximum queue capacity is reached, subsequent tasks will be rejected.- Parameters:
maximumQueueSize
- the capacity of the queue.threadPrefix
- the name prefix for the thread created by this executor.loggerIdentity
- the class to use as logger name for logging uncaught exceptions from the threads.- Returns:
- a new ExecutorService with the specified configuration.
-