Implement Thread Pool in Java

In my previous post, I talked about the basic idea of Thread pool, its pros and cons and why we should use it. This post will cover the high level overview about the implementation of Thread Pool.

You don’t need to create your own thread pool to manage threads. Java 5 comes with the package java.util.concurrent which provides implementation of Thread pool.

Java thread pool manages the collection of Runnable threads and worker threads execute Runnable from the queue. java.util.concurrent.Executors provide implementation of java.util.concurrent.Executor interface to create the thread pool in java. Let’s write a simple program to explain it’s working.

First we need to have a Runnable class, named ThreadProcessor.java

package threadpool;

public class ThreadProcessor implements Runnable
{

	private int threadCount;

	public ThreadProcessor(int threadCount) {
		this.threadCount = threadCount;
	}

	@Override
	public void run()
	{
		System.out.println("Number " + threadCount + " is running");

		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.println("Number " + threadCount + " has been stopped");
	}

}

This is Runnable class in which there is basic implementation of run() method. Following is the Executor class which will create a thread pool and execute above Runnable class.

package threadpool;

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

public class ThreadPoolExample
{
	public static void main(String[] ar)
	{
		ExecutorService executor = Executors.newFixedThreadPool(5);//newFixedThreadPool(5) will create a fixed size thread pool with five working threads

		for(int i=0;i<10;i++)
		{
			executor.execute(new ThreadProcessor(i));//It will execute the runnable class
		}

		executor.shutdown(); //It will shutdown the executor when all the working threads are done with their jobs

		while (!executor.isTerminated()) {

		}
		System.out.println("Finished all threads");
	}
}

In above example, it will create a thread pool with 5 working threads and we are executing 10 jobs in it. It will execute 5 jobs in 5 different threads and other jobs will wait until threads are not free. As soon as a thread gets free, it will pickup a job from the job queue and will perform task.

Following is the output of the above program:

Number 0 is running
Number 4 is running
Number 3 is running
Number 1 is running
Number 2 is running
Number 3 has been stopped
Number 4 has been stopped
Number 0 has been stopped
Number 2 has been stopped
Number 1 has been stopped
Number 8 is running
Number 7 is running
Number 6 is running
Number 5 is running
Number 9 is running
Number 7 has been stopped
Number 9 has been stopped
Number 5 has been stopped
Number 6 has been stopped
Number 8 has been stopped
Finished all threads

As you can see in the output, it executed 5 task at a time and then pickup next 5 task to execute. This was the basic implementation of Thread pool in java. Read more about the thread pool.

Advertisements

2 thoughts on “Implement Thread Pool in Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s