Java Language Use cases for different types of ExecutorService


Example

Executors returns different type of ThreadPools catering to specific need.

  1. 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) and newSingleThreadExecutor() as the java doc 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 newFixedThreadPool can be reconfigured later in the program by: ((ThreadPoolExecutor) fixedThreadPool).setMaximumPoolSize(10) This is not possible for newSingleThreadExecutor

    Use cases:

    1. You want to execute the submitted tasks in a sequence.
    2. You need only one Thread to handle all your request

    Cons:

    1. Unbounded queue is harmful
  2. 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:

    1. Effective use of available cores. Configure nThreads as Runtime.getRuntime().availableProcessors()
    2. When you decide that number of thread should not exceed a number in the thread pool

    Cons:

    1. Unbounded queue is harmful.
  3. 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:

    1. For short-lived asynchronous tasks

    Cons:

    1. Unbounded queue is harmful.
    2. Each new task will create a new thread if all existing threads are busy. If the task is taking long duration, more number of threads will be created,which will degrade the performance of the system. Alternative in this case: newFixedThreadPool
  1. 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:

    1. Handling recurring events with delays, which will happen in future at certain interval of times

    Cons:

    1. Unbounded queue is harmful.

    5.public static ExecutorService newWorkStealingPool()

    Creates a work-stealing thread pool using all available processors as its target parallelism level

    Use cases:

    1. For divide and conquer type of problems.
    2. Effective use of idle threads. Idle threads steals tasks from busy threads.

    Cons:

    1. Unbounded queue size is harmful.

You can see one common drawbacks in all these ExecutorService : unbounded queue. This will be addressed with ThreadPoolExecutor

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, 
TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
RejectedExecutionHandler handler)

With ThreadPoolExecutor, you can

  1. Control Thread pool size dynamically
  2. Set the capacity for BlockingQueue
  3. Define RejectionExecutionHander when queue is full
  4. CustomThreadFactory to add some additional functionality during Thread creation (public Thread newThread(Runnable r)