A thread pool is a group of threads that are created in advance and stored in a pool (like a queue). When needed, the threads are taken out from the pool and assigned to tasks. After finishing their task, they are returned back to the pool.
Thread pools are used to reduce the overhead of thread creation and management. Using a thread pool, we can avoid creating and destroying threads frequently, which reduces the performance overhead.
Executors
classThe Executors
class provides several static methods that can be used to create and manage a thread pool. The most commonly used method is the newFixedThreadPool()
method, which creates a fixed-size thread pool.
Let's see how to use the newFixedThreadPool()
method to create a thread pool.
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;class ThreadPoolExample {public static void main(String[] args) {// create a fixed size thread pool with 5 threadsExecutorService service = Executors.newFixedThreadPool(5);// submit tasks to be executed by the poolfor (int i = 0; i < 10; i++) {service.submit(new Task(i));}// shutdown the poolservice.shutdown();}}class Task implements Runnable {private int taskId;public Task(int id) { this.taskId = id; }public void run() {System.out.println("Starting task " + taskId);try {Thread.sleep(100);}catch (InterruptedException e) {e.printStackTrace();}System.out.println("Finishing task " + taskId);}}
ExecutorService
and Executors
classes.ThreadPoolExample
class.Task
class that implements Runnable
. A task is just a Runnable
that we submit to an ExecutorService
.taskId
field that is used to track the task.id
and sets the taskId
field.run()
method is where we implement the task logically. In this example, we just print a message and sleep for 100 milliseconds.The newSingleThreadExecutor()
methods of Executors
class creates an executor with a single thread. The example below demonstrating the use of this method:
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;class ThreadPoolExample {public static void main(String[] args) {// create a new executor with a single threadExecutorService service = Executors.newSingleThreadExecutor();// submit tasks to be executed by the poolfor (int i = 0; i < 10; i++) {service.submit(new Task(i));}// shutdown the poolservice.shutdown();}}class Task implements Runnable {private int taskId;public Task(int id) { this.taskId = id; }public void run() {System.out.println("Starting task " + taskId);try {Thread.sleep(100);}catch (InterruptedException e) {e.printStackTrace();}System.out.println("Finishing task " + taskId);}}
ExecutorService
using the newSingleThreadExecutor()
method.Task
class that implements Runnable
. The newCachedThreadPool()
creates a thread pool that uses a limited number of threads that are kept alive as long as there are tasks to execute.
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;class ThreadPoolExample {public static void main(String[] args) {// create a new cached thread poolExecutorService service = Executors.newCachedThreadPool();// submit tasks to be executed by the poolfor (int i = 0; i < 10; i++) {service.submit(new Task(i));}// shutdown the poolservice.shutdown();}}class Task implements Runnable {private int taskId;public Task(int id) { this.taskId = id; }public void run() {System.out.println("Starting task " + taskId);try {Thread.sleep(100);}catch (InterruptedException e) {e.printStackTrace();}System.out.println("Finishing task " + taskId);}}
newCachedThreadPool()
method. This will create a pool of threads that can grow and shrink as needed.Task
class that implements Runnable
.