Java Multithreading, Concurrency, and Parallelism — Part 21.1

7 min readApr 10, 2022

The Introduction to Executor Framework

Do you visualize this as a Thread Pool

Have you ever wondered about people talking the ThreadPools, ExecutorServices, Callables, Futures, and SeprationOfConcerns? And you want to know about these in-depth. Then this article is for you.

Java Concurrency Framework is a collection of micro-frameworks. One of them is the Executor Framework. The Java Executor Framework deals with distributing the tasks to the threads for their execution. But why there is a need for another framework just to run these tasks by the threads. Why can’t we simply create the threads with the Runnable or Callable representing the tasks? Well, there is the main disadvantage with it. So let’s just understand these disadvantages before delving into the Executor framework.

Suppose let’s say we want to have a simple web server that handles the requests from the clients over the socket. We can create a simple server as below.

1 ServerSocket socket = new ServerSocket(6000);
2 while (true) {
3 Socket connection = socket.accept();
4 handleRequest(connection);
5 }

The above logic works in a sequential manner, in other words, it is Single-Threaded. If two clients request at the same, they will get processed one after the other. While the server is handling a request, new connections must wait
until it finishes the current request and calls accept again. So whoever did not get the first chance will have to wait for the other request to be completed. While this logic is correct but this does not work well in production as it can handle only a single request at a time. This exhibits very poor performance and is not at all scalable. If there are 100 clients firing the requests, they keep on waiting for the other requests to be completed resulting in a very poor user experience. So that gives us where the problem lies. But let’s understand the problem furthermore.

In the above scenario, handling the client request involves many tasks that are a mix of both I/O and Computation. I/O can either be a Network I/O or Disk I/O. In general, the following steps may be involved to handle a request from the client.

  1. First, the server reads the data from the socket stream. This is a Network I/O.

Tech Lead | Full Stack Developer | Software Consultant | Technical Content Writer | Free Lancer