What is Multithreading in javaMultithreading in java is must to know concept. Multithreading is not just a concept, it is complete system which you need to understand. In this tutorial, we will first discuss about the concept of Multithreading in java. After that we will discuss about it’s advantage and how it is useful in distributed systems and much more.
MultithreadingLet’s take a real world example on multi threading.
You are planning to watch a good movie tonight with your friends. So you called your friends and they all are ready to come to your place to enjoy. After all, your friends are coming so it’s your responsibility to bring some food and drinks for them. You know it well, they are not going to bring anything.
Time has come and all of your friends came to your place. You all decided to watch a great movie after a long discussion. You told each friend that food and drinks are in the kitchen, if anyone hungry then take it yourself.
You and your friends took food and drinks by their self and sat down in front of the TV and begin to watch the movie.
Now let me tell you what’s happening here.
Watching a move is a task. You and your friends are process of the task which you can say Thread Based Multi Processing One of your friend is not watching the movie anymore because he is talking with her girlfriend is another thread. Another one is checking his social profile is another thread. One friend is sleeping because he took lots of drink is another thread. And this process is called multi threading.
So Multithreading is a concept or process in which multiple threads are running simultaneously to perform some action. And Thread is the part of the process.
Don’t be confuse between Multi Threading and Multi TaskingSometimes it is confusing, what is multi threading and what is multi tasking. So let me clear the different between them.
You are working on some project on your laptop, this is one task. At the same time you are listening music, this is completely another task. Now when you are writing some code in some IDE, some validations are happening, code is building automatically, when you type something auto sensing is providing you some suggestions on code. These all are threads.
Threads are lightweight process and are the part of some task. When you perform a task, multiple threads are also working together to provide great performance and output.
Than what about Multi ProcessingMultiprocessing and Multithreading both are the part of multi tasking. Both are used to achieve multi tasking. But we choose multi threading over multi tasking because Multi processing does not share the common memory area, each process takes it’s own space in memory which increases the memory consumption.
But in Multi threading, threads share the common memory area which helps to utilize memory space in proper manner.
So Multi Threading is a light weight sub process of a Task which executes simultaneously to perform some action. Multi Threading and Multi Processing both are used to achieve Multi Tasking. Thread is the part of the process or sometimes called sub process.
Why should we use Multi Threading ?
What are the advantages of using Multi Threading in java programming language. Let’s find out why ?
- Enhanced performance on multi processor systems.
- It allows better resource utilization.
- Simultaneous access to multiple applications at the same time.
- Reduces the number of servers required in distributed environments.
- Improved GUI response.
- Simultaneous and parallelized occurrence of tasks.
- Better use of CPU resource.