Types of Thread Pool Executors
Thread Pool Executor separates the task of creating and maintaining the thread task through its lifecycle, so the developer doesn’t need to focus on maintaining the task executor rather than on the actual business use case.
Thread Pool Executor class implements both the ExecutorService interface and its parent Executor as well.
For different use cases, thread pool executor provides different types of executor services which are briefly described below -
1. Fixed Size Thread Pool Executor:
This is the most common thread pool executor which is being used in most of the use cases. This thread pool executor needs to be configured with the initial nos. of threads and the total nos. of threads.
So during startup, this executor starts with the minimal nos. of threads and depends upon the increasing load it increases the nos. of threads till the maximum no. configured. After that limit, if a new task arrives it will wait in the waiting queue unless any thread gets free from its task.
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
2. Cached Thread Pool Executor:
Creates a thread pool that creates the thread on demand, i.e unlike fixed size thread pool executor it creates the new threads if a new task arrives and there are no existing free threads available in the pool.
Creating of new threads is not limited, it is only limited till memory supports creating new threads. If any thread is created but is in an idle state for more than 60 seconds, the thread will get terminated from the thread pool
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
3. Scheduled Thread Pool Executor:
It is a kind of fixed thread pool executor with pre-defined nos. of threads available, but in this case, the scheduled thread pool is configured to execute the task at a certain time or after a certain time period.
Recommended by LinkedIn
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newScheduledThreadPool(10);
4. Single Thread Pool Executor:
This kind of thread pool executor contains a single thread to handle the tasks. This kind of executor is useful when the incoming task is a very less amount in a particular time frame or the result of a task is not needed ASAP, in these cases, we can think of creating this single thread pool executor to let a single thread to take care all the tasks one by one.
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newSingleThreadExecutor();
5. Work Stealing Thread Pool Executor:
This kind of thread pool works on the principle of parallelism of a task by forking a task and then joining at the end. It maintains enough threads to support multi-processor processing.
In Java7 we had ForkJoinPool which was working on the same principle. From java8 onwards this work-stealing thread pool executor has been introduced which is ultimately using the same fork-join pool with some pre-defined configuration.
This is named work-stealing because a task can be forked into multiple tasks and it will be shared in a pool which is accessed by a different processor. So, once a task is pushed into this pool and there is a thread available in another processor may take up (steal) the task and continue with it.
Below example will create a work stealing thread pool which will be shared among 4 cores of a CPU.
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newWorkStealingPool(4);