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:
- Why do we use multithreading?
- What problems does multithreading solves?
- 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:
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.
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.
NO UI FREEZING.
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:
- Using all the available processors
- 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:
- Using all the available processors
- 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:
- Picks a thread from already initialized pool of threads.
- Gives the request to the thread.
- 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
- A multi-threaded application can utilize the CPU potential to the fullest .
- An application can be made better responsive if we use multithreading.
- Development and Maintenance cost might or might not be reduced.
- 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.