In our previous article we took a look on what are all the advantages of using multithreading in Java. But is everything good about multithreading or are there any disadvantages of multithreading also?
Because if there are, then it becomes very important to understand those disadvantages. This will make us see:
- If we want to use multithreading or not
- 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:
- When not to use multithreading.
- If you do, what issues you might face.
- What are all the disadvantages of multithreading?
So, stay tuned!!
This article is part of our series on multithreading in java.
Disadvantages of Multithreading in Java
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.
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.
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.
In this issue, a thread is on wait for CPU cycles. Thread scheduler somehow does not executes a thread.
Key Point of Multithreading Disadvantages
- Multi threaded application is comparatively always difficult to understand.
- Also it is more prone to the issues. Deadlock and livelock are good examples of this.
- If we do not optimize the number of threads, instead of performance gain, it can have performance hit.
- Last but not the least, it can bring sweat and pain while debugging a multithreading application.
So, everything is not good about multithreading application.