Multithreading Advantages in Java – Why to use?

Till our previous article we got well versed in different terminologies of Java multithreading. But why to go through such a mess? Why to learn so much complex topic in the first place? What are the advantages of multithreading?

Before starting with the topic of multithreading and then going into the details, it is very reasonable question to ask as to why we use multithreading in Java in the first place?

As Java multithreading is considered one of the most complex topics, even for the experienced Java developers, we should know if there is a way around to it.

After this article you will know:

  1. Why do we use multithreading?
  2. What problems does multithreading solves?
  3. What are the advantages of multithreading?

So, stay tuned!!

Current article is part of our course on multithreading in Java.

Advantages of Multithreading in Java

1. Improves the responsiveness of User Interface

We will go through different examples of responsiveness to understand this:

Example #1

In our earlier articles we have discussed the example of query handler. There we could see that while query processing is going on, in a non-concurrent system, user is not able to cancel it. This is not a very good user experience.

On the other hand when we converted this application to concurrent application, it could respond to the cancel request while query execution is going on in a different thread.

Application was responsive to accept the cancel request. Better User experience.

Example #2

Java Swing works on the single thread event loop. If we write anything which requires high computation time, our whole swing UI will freeze.

All the high computation tasks should happen in different threads. After the task is complete, we should push the result to the event loop thread to use it.


Fun Fact: JavaScript engine also works in a similar manner.

Example #3

Do you know in a web server like Apache or Tomcat, every user request is handled in a separate thread?

Can you imagine the scenario where the user has to wait for other user’s requests to get complete?

2. Better Resource Utilization

This advantage is one of the most obvious one. For an IT tech company, one of the bigger chunk of expenditure goes into the provisioning systems on which their applications will run.

An application which is using 2 out of the 12 available processors can hit the company’s pockets very bad.

In this way, this badly written application requires 12/2 = 6 machines while all the work could be done on a single machine.

NOW, this again has two deeper prospective to understand:

  1. Using all the available processors
  2. Using each of the processors in an efficient way.

First point should be clear enough as already explained in the example that we need to enable the application to utilize all available 12 processors.

Now to the second point: Even if a thread is using a processor continuously it might be blocked on the I/O to work. This I/O handling should also be considered and is of utmost importance.(Think of Reactive programming, event-driven designs, asynchronous promises here.)

So, application should schedule other thread to utilize this processor while current thread is blocked on the I/O.

3. Performance Improvement

This multithreading advantage comes as a derivative of better utilization of resource. Difference is that we are now thinking from the perspective of application rather than company.

Multithreading help achieve better throughput by:

  1. Using all the available processors
  2. Using the optimized I/O processing to further utilize CPU cycles in a better way.

4. Simplification of Application Modelling

Sometimes the solution to a problem is easy to build, understand and execute if we do that in multi-threaded way.

Again, take the example of tomcat server which solving the problem of handling HTTP requests concurrently.

Whenever an HTTP request comes, server:

  1. Picks a thread from already initialized pool of threads.
  2. Gives the request to the thread.
  3. Once done, releases the thread back to the pool.

100 different HTTP requests => 100 different threads. Very simple, right?

Every solution has pros and cons but right now just grab the point of the simplicity of the solution.

5. Reduces Development and Maintenance Cost

This statement is not 100% true.

For a certain set of problems, if we design a multi-threaded solution, it might give us benefit in the overall development and maintenance cost.

BUT, multi-threaded solutions are known to be hard to debug, giving incorrect behavior etc. (We will discuss these in next article).

So, it might or might not reduce the development and maintenance cost.

Key points on multithreading advantages

  1. A multi-threaded application can utilize the CPU potential to the fullest .
  2. An application can be made better responsive if we use multithreading.
  3. Development and Maintenance cost might or might not be reduced.
  4. For a set of problems, multithreading can provide a better design solution but for other problems, it might not.

What to read next?

Is everything good about multithreading? You must have already heard some bad things about multithreading, isn’t it? Now, let’s understand the disadvantages of using multithreading now.

Leave a Comment