multithreadingExecutors


Syntax

  • ThreadPoolExecutor

  • ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)

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

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

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

  • Executors.callable(PrivilegedAction<?> action)

  • Executors.callable(PrivilegedExceptionAction<?> action)

  • Executors.callable(Runnable task)

  • Executors.callable(Runnable task, T result)

  • Executors.defaultThreadFactory()

  • Executors.newCachedThreadPool()

  • Executors.newCachedThreadPool(ThreadFactory threadFactory)

  • Executors.newFixedThreadPool(int nThreads)

  • Executors.newFixedThreadPool(int nThreads, ThreadFactory threadFactory)

  • Executors.newScheduledThreadPool(int corePoolSize)

  • Executors.newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)

  • Executors.newSingleThreadExecutor()

  • Executors.newSingleThreadExecutor(ThreadFactory threadFactory)

Parameters

ParameterDetail
corePoolSizeMinimum number of threads to keep in the pool.
maximumPoolSizeMaximum number of threads to allow in the pool.
keepAliveTimeWhen number of threads is greater than the core, the noncore threads (excess idle threads) will wait for time defined by this parameter for new tasks before terminating.
unitTime unit for keepAliveTime.
timeoutthe maximum time to wait
workQueueThe type of queue that our Executor is going to use
threadFactoryThe factory to use when creating new threads
nThreadsThe number of threads in the pool
executorThe underlying implementation
taskthe task to run
resultThe result to return
actionThe privileged action to run
callableThe underlying task

Remarks

The different types of Threadpools and Queues that are explained below, have been taken from the information and knowledge from [oracle documentation][1] and [Jakob Jenkov][2] blog where you can learn a lot about concurrency in java.

Different types of ThreadPools

SingleThreadExecutor: Executor that uses a single worker thread operating off an unbounded queue, and uses the provided ThreadFactory to create a new thread when needed. Unlike the otherwise equivalent newFixedThreadPool(1, threadFactory) the returned executor is guaranteed not to be reconfigurable to use additional threads.

FixedThreadPool: thread pool that reuses a fixed number of threads operating off a shared unbounded queue, using the provided ThreadFactory to create new threads when needed. 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. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.

CachedThreadPool: Thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available, and uses the provided ThreadFactory to create new threads when needed.

SingleThreadScheduledExecutor: Single-threaded executor that can schedule commands to run after a given delay, or to execute periodically. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newScheduledThreadPool(1, threadFactory) the returned executor is guaranteed not to be reconfigurable to use additional threads.

ScheduledThreadPool: Thread pool that can schedule commands to run after a given delay, or to execute periodically. Different types of Work Queues