Use cases for different types of ExecutorService
suggest changeExecutors returns different types of thread pools catering to a specific need.
-
public static ExecutorService newSingleThreadExecutor()Creates an Executor that uses a single worker thread operating off an unbounded queue
There is a difference between
newFixedThreadPool(1)andnewSingleThreadExecutor()as the Javadoc says for the latter:Unlike the otherwise equivalent
newFixedThreadPool(1), the returned executor is guaranteed not to be reconfigurable to use additional threads.Which means that a
newFixedThreadPoolcan be reconfigured later in the program by:((ThreadPoolExecutor) fixedThreadPool).setMaximumPoolSize(10)This is not possible for
newSingleThreadExecutor.Use cases:
- You want to execute the submitted tasks in a sequence.
- You need only one thread to handle all your requests.
Cons:
- The unbounded queue is harmful.
-
public static ExecutorService newFixedThreadPool(int nThreads)Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. 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
Use cases:
- Effective use of available cores. Configure
nThreadsasRuntime.getRuntime().availableProcessors() - When you decide that the number of threads should not exceed a number in the thread pool
Cons:
- The unbounded queue is harmful.
- Effective use of available cores. Configure
-
public static ExecutorService newCachedThreadPool()Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available
Use cases:
- For short-lived asynchronous tasks
Cons:
- There is no bound on the number of threads.
- Each new task will create a new thread if all existing threads are busy. If the task takes a long time, more threads are created, which degrades the performance of the system. Alternative in this case:
newFixedThreadPool
-
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.
Use cases:
- Handling recurring events with delays, which will happen in the future at certain intervals
Cons:
- The unbounded queue is harmful.
-
public static ExecutorService newWorkStealingPool()Creates a work-stealing thread pool using all available processors as its target parallelism level
Use cases:
- For divide and conquer type of problems.
- Effective use of idle threads. Idle threads steal tasks from busy threads.
Cons:
- The pool does not use one shared unbounded queue; each worker has its own deque.
One common drawback of the single-thread, fixed, and scheduled pools is an unbounded queue. This is addressed with ThreadPoolExecutor
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
RejectedExecutionHandler handler)
With ThreadPoolExecutor, you can
- Control Thread pool size dynamically
- Set the capacity for
BlockingQueue - Define a
RejectedExecutionHandlerwhen the queue is full - Use a
ThreadFactoryto add some additional functionality during thread creation (public Thread newThread(Runnable r))