The Thread Pool Pattern

DHolness
3 min readMar 6, 2016

--

My current apprentice project is to build a basic web server using Java. One of the requirements for my server is the ability to manage request concurrently. One of the acceptance test requires the server to handle 4,096 request. Which is enough to break a single threaded sever. After doing some research I found an ideal solution, the Thread Pool Pattern.

What is a Thread?

So the first question, what is a thread ? A thread is the smallest unit of processing that can be performed by an operating system. Threads are also defined as lightweight process. In java, a process is a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space. Threads allow us to define several light weight process that can excuse independently and if need concurrently(system permitting).

What is Multithreading?

Multithreading is the ability of a CPU to execute multiple processes or threads concurrently. Multithreading at its simplest definition allows you to do several lightweight process at once. It’s like turning a one way street into a four lane highway, allowing multiple cars to process the same distance at the same time rather than being queued behind each other.

The Thread Pool Pattern

The Thread Pool pattern is a simple pattern for managing concurrent request. The thread pool allows you to handle multiple request efficiently and with needed constraint. The pool pattern saves on overhead from thread object creation and limits the available resources. Allowing for the system to handle request within its limits.

Looking at the diagram above we can see task are placed into one of the thread slots within the pool. Once the task is completed the pool slot opens up for the next task in the queue. Running at full capacity six threads can be running at a single instance.

Sample Implementation

Now that we have a brief understanding of the thread pool we can look at a sample implementation from my web server project.

To understand example lets first look at line 24 and work our way back. On line 24 we have several things going on. First the protected ExecutorService object threadPool is calling execute with a Client worker services object argument. The Client worker service object is a class that implements the Runnable interface. The Runnable interface is implemented by any class whose instances are intended to be executed by a thread. In this example our workers are responsible for handling request and are processing these requests in independent threads.

Next if we turn to line 11, we will see the thread pool is created by using the Executors static method newfixedthreadPool(). This is where we define the constraints of our pool. In this example our server can only operate 10 threads at a give moment. We allow for concurrency, but within limits.

Once a server request is accepted(line 23) and threadPool calls execute, if no threads are available the request will have to wait until there is an open resource. As a result we have concurrency with 10 threads running at any given time, but at a limit of 10.

The thread pool pattern allows for us to mange the overhead concerning the creation and destruction of threads. In addition, it provides better performance and system stability. What to learn more about concurrency in java? Take a look at the this link.

--

--