Disadvantages of Multithreading in Java

Hey guys! Let’s continue with our series on Multithreading In Java.

Earlier, in our previous article we did take a look at all the possible advantages of multithreading in Java, in a comprehensible manner. There we saw how using it makes your application at super advantage by smartly tapping on the JVM platform features to make best use of underlying processors.

Now, this begs the question:

Is everything shiny and great about multithreading? Is it why it is the hot topic for every company in the interviews? That’s why a developer is treated at an advantage who knows multithreading well?

Now, what if there are so many disadvanages of multithreading which basically squares off all the advantages of it?

What if there is an ugly side to it capable of making all the advantages pretty much unacceptable or useless to the developers?

Because if there are, then it becomes very important to understand those disadvantages.

Understanding these disadvantages will make us decide:

  1. If we want to use multithreading or not
  2. And if we use, what all things should be considered while implementing multithreading.

And this is exactly what we will discuss in this article.

After this article you will understand:

  1. When not to use multithreading.
  2. If you do, what issues you might face.
  3. What are all the disadvantages of multithreading?
  4. Do you want to spend time learning it or not.

So, stay tuned!!

Disadvantages of Multithreading in Java

You might ask: Wait a minute! Why are we learning about disadvantages of multithreading in Java without even experienced it ourselves?

Shouldn’t we try using it first, spend a bit time on it, give it a fair trial and then make a judgement call on it?

While the logic is absolutely right but do we want to spent too much time on it, later to find out that it was not a good fit for us? And mind you, we can not know about the negative side of it until we spent a really good amount of time.

So, what if we take the opinions of other experienced developers who have worked on multithreaded applications which are battle tested in production and see how they feel about it?

Seems agreeable.

So, here are the few pain points of experienced developers while using multithreading.

1. Difficult to understand application

If you have already gone through at-least one of the multi-threaded application, you know how hard it can be to understand what exactly is going on.

Due to the nature of the asynchronous tasks in multi-threading, it is very difficult to understand the flow of the application just by reading the code.

2. Prone to incorrect behavior

Every application is prone to incorrect behavior but multi-threaded applications are worse in this.

A multi-threaded applications consists of many critical sections, locks, thread pools, semaphores, mutable data structures etc. [Don’t worry, we will study all these in detail]

There is a very high probability that something will go wrong while using all these different techniques. Even if it seems like all things are in place, it might not be so.

Also the application might be working correct right now, but later on it might produce different behavior. That is one of the most frustrating part of a multi-threaded application.

3. Hard to debug

Issues occurs in every software application. But what makes multithreading such a giant pain is that issues are hard to debug in here.

Most of the time issue happens because of the race conditions which are not easy to replicate.

And if they replicate, you will have the debug the flow while going through multiple threads in the debug explorer. How to debug a multi-threaded application is an art in itself.

If you don’t understand the application, it will be even worse as you will not be clear about where to put the breakpoints in the first place.

4. Performance overhead with Context Switching

There is high degree of cost involve in thread scheduling and context switching.

If the number of threads are not optimized according to the underlying operating system and hardware, our idea of performance enhancement can backfire.

It takes time to save and restore a thread’s execution context. So, a thread scheduler might by spending most of it’s time in scheduling of the thread instead of executing it.

5. Deadlock

This is an issue wherein application is not able to move forward.

In a sequential executing application, this problem can never arise. Only in a multi-threaded application using locks, threads are stuck on the each other locks.

6. Livelock

This is another issue complementary to deadlock where application can not move forward because both are trying to get to the execution at the same time.

7. Starvation

In this issue, a thread is on wait for CPU cycles. Thread scheduler somehow does not executes a thread.

Key Point of Multithreading Disadvantages

  1. Multi threaded application is comparatively always difficult to understand.
  2. Also it is more prone to the issues. Deadlock and livelock are good examples of this.
  3. If we do not optimize the number of threads, instead of performance gain, it can have performance hit.
  4. Last but not the least, it can bring sweat and pain while debugging a multithreading application.

Leave a Comment