What is a Thread pool in Java?

Creating a new thread for every job may create performance and memory problems. To overcome this we should go for the Thread pool in Java.

  • Thread Pool is a pool of already created threads ready to do our job. 
  • Java 1.5v introduced the thread pool framework to implement the thread pool.
  • The Thread pool in java is also called the Executor Framework.
  • We can create a Thread pool as follows.
    • ExecutorService service = Executors.newFixedThreadPool(5);
  • Submit a Runnable job by using submit method as follows.
    • service.submit(job);
  • We can shut down the Executor service by using the shutdown method.
    • service.shutdown();

Let’s see the following example for the Thread Pool in Java.

From the following example we created the pool size of 3, so at a time max 3 threads will be running. Once we submit the thread job in the service, it will be executed and run method will be called automatically.

Java Code
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Test {
public static void main(String args[]) throws InterruptedException {
MyThread t1 = new MyThread(“Thread–1”);
MyThread t2 = new MyThread(“Thread–2”);
MyThread t3 = new MyThread(“Thread–3”);
MyThread t4 = new MyThread(“Thread–4”);
MyThread t5 = new MyThread(“Thread–5”);

ExecutorService service = Executors.newFixedThreadPool(3);

service.submit(t1);
service.submit(t2);
service.submit(t3);
service.submit(t4);
service.submit(t5);

}
}

class MyThread extends Thread {
String name;

public MyThread(String name) {
this.name = name;
}

public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(“Running ” + name + “: ” + i);
}

System.out.println(name + ” Completed.”);
}
}

Output:
Running Thread–3: 0
Running Thread–2: 0
Running Thread–2: 1
Running Thread–1: 0
Running Thread–2: 2
Running Thread–3: 1
Running Thread–2: 3
Running Thread–1: 1
Running Thread–2: 4
Running Thread–3: 2
Thread–2 Completed.
Running Thread–1: 2
Running Thread–3: 3
Running Thread–1: 3
Running Thread–1: 4
Running Thread–3: 4
Thread–1 Completed.
Thread–3 Completed.
Running Thread–4: 0
Running Thread–5: 0
Running Thread–4: 1
Running Thread–5: 1
Running Thread–4: 2
Running Thread–5: 2
Running Thread–4: 3
Running Thread–5: 3
Running Thread–4: 4
Running Thread–5: 4
Thread–4 Completed.
Thread–5 Completed.

Advantages of a Thread Pool in java:

  • Thread pool saves time since multiple threads are running at the same time.
  • Since multiple threads can be run at a time we can have Better performance.
  • We can restrict the maximum running threads at a time by giving the number while creating the executer service.
  • Easy to access and real-time usage.

Disadvantages of the Thread Pool

  • We can not control which thread needs to be executed first hence no track can be kept.
  • No control over the priority and state of the thread.
  • Since we don’t have control over which threads can be executed first, it may affect the program if any update operation should be performed before selecting. 

-A blog by Shwetali Khambe

Related Posts