Skip to content

ThreadPoolExecutor

Vishnu Garg edited this page Aug 15, 2018 · 2 revisions

The java.util.concurrent.ThreadPoolExecutor is an implementation of the **ExecutorService **interface. The **ThreadPoolExecutor **executes the given task (Callable or Runnable) using one of its internally pooled threads.

The thread pool contained inside the ThreadPoolExecutor can contain a varying amount of threads. The number of threads in the pool is determined by these variables:

  • corePoolSize
  • maximumPoolSize

If less than corePoolSize threads are created in the the thread pool when a task is delegated to the thread pool, then a new thread is created, even if idle threads exist in the pool.

If the internal queue of tasks is full, and corePoolSize threads or more are running, but less than maximumPoolSize threads are running, then a new thread is created to execute the task.

Here is a diagram illustrating the ThreadPoolExecutor principles: Thread Pool

Creating a ThreadPoolExecutor*

int  corePoolSize  =    5;
int  maxPoolSize   =   10;
long keepAliveTime = 5000;

ExecutorService threadPoolExecutor =
        new ThreadPoolExecutor(
                corePoolSize,
                maxPoolSize,
                keepAliveTime,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>()
                );

Refrences https://dzone.com/articles/scalable-java-thread-pool-executor

Clone this wiki locally