Thread pool is a core concept in multithreaded programming that serves a collection of idle threads that can be used to execute a task. A thread pool can reuse previously created threads to execute the current task so that the thread is already available when the request arrives, which can reduce the time of thread creation and improve the performance of the application. Normally, thread pool can be used in a web server to handle client requests and also to maintain open connections to the database.
We can configure the maximum number of concurrent threads in the pool, which is useful for preventing overload. If all threads are busy executing a task, then a new task is placed in a queue and waits for a thread to become available.
The Java concurrency API supports the following types of thread pools:
Fixed-thread pool: A thread pool with a fixed number of threads. A task will only execute if a thread is available, otherwise, it is waiting in a queue. The Executors.newFixedThreadPool() method is used to create a fixed-thread pool.
Cached-thread pool: A thread pool where we can create new threads as required, but also reuse previously created threads. A thread will be terminated and removed from the pool if it is ideal for 60 seconds. The Executors.newCachedThreadPool() method is used to create a cached-thread pool.
Single-thread pool: A thread pool with one thread. It executes tasks one by one. The Executors.newSingleThreadExecutor() method is used to create a single-thread pool.
Fork/join pool: A thread pool that is used to perform heavy tasks faster, by splitting the task into smaller pieces recursively. To create a fork/join pool, we need to create an instance of the ForkJoinPool class.