Hello everyone, today we are going to discuss one of the most important concepts in Java - Multithreading.
In the first part of this multi-part blog series, we will be discussing the basics of threads, how can we create them? which is the best approach? and many other questions like these. So without further ado, let's get started.
What do we understand from this story? Look how Batman divided the tasks between himself and Robin. This made them do two tasks simultaneously and in the half amount of time. This is what threads do - they perform tasks concurrently.
Multithreading allows executing different parts of a program to achieve the maximum CPU utilization. Few properties of threads are -
In the first part of this multi-part blog series, we will be discussing the basics of threads, how can we create them? which is the best approach? and many other questions like these. So without further ado, let's get started.
What are threads?
Let's say you are Batman (pretty cool, eh? 😊) and your arch-enemy, the master alchemist Ra's al Ghul has brewed a dangerous potion from the Lazarus Pit. To avenge his earlier defeats by the hands of The Dark Knight, he has spread the potions all over the Gotham city.
Now, the only way to save citizens of Gotham is to find the cure of the potion (hidden in the city somewhere) and find Ra's as well before he can devise a new plan to destroy the city.
Batman calls Nightwing (the original Robin) and asks his help in finding Ra's. Batman decides to find the cure of the deadly potion.
In the end, both Batman and Robin were able to locate the cure and Ra's successfully and saved the day.What do we understand from this story? Look how Batman divided the tasks between himself and Robin. This made them do two tasks simultaneously and in the half amount of time. This is what threads do - they perform tasks concurrently.
Multithreading allows executing different parts of a program to achieve the maximum CPU utilization. Few properties of threads are -
- We can do multiple activities within a single process.
- Light-weight process.
- A thread shares the memory with other threads.
- But each thread has a separate program counter, stack and local variables.
Creating a thread
We can create threads in two ways
- Extending the Thread class
- Implementing the Runnable interface
Below code shows both the ways of creating a thread -
As you can see that in the first approach we are extending the Thread class and overriding the run() method. While in the second approach we are implementing the Runnable interface and implementing the run() method.
Then from our main class, we are calling the start() method to invoke the threads. Behind the scenes, the start() method calls the run() method.
After running the code, we will see the following output
Thread created by implementing Runnable interface 14 is running
Thread created by extending Thread class 13 is running
Thread created by extending Thread class 15 is running
Thread created by implementing Runnable interface 16 is running
Thread created by extending Thread class 17 is running
Thread created by implementing Runnable interface 18 is running
Then from our main class, we are calling the start() method to invoke the threads. Behind the scenes, the start() method calls the run() method.
After running the code, we will see the following output
Thread created by implementing Runnable interface 14 is running
Thread created by extending Thread class 13 is running
Thread created by extending Thread class 15 is running
Thread created by implementing Runnable interface 16 is running
Thread created by extending Thread class 17 is running
Thread created by implementing Runnable interface 18 is running
Thread class vs Runnable interface
- The basic difference between Thread and Runnable is that each thread defined by extending the Thread class creates a unique object and get associated with that object. On the other hand, each thread defined by implementing Runnable interface shares the same object.
- Since a unique object is created while extending the thread class, hence more memory is required. But if we implement the Runnable interface, this will not happen.
- If we extend the Thread class then we cannot extend any other class as Java does not support multiple inheritance. But if we implement the Runnable interface then we can extend other class as well as implement other interfaces.
- We should extend Thread class only if we have to override or specialise some other methods of Thread class. We must implement a Runnable interface if we only want to specialise run method only.
- Extending the Thread class introduces tight coupling in the code as the code of Thread and job of the thread is contained by the same class. On the other hand, Implementing Runnable interface introduces loose coupling in the code as the code of Thread is separate from the job assigned to the thread.
This comment has been removed by the author.
ReplyDeleteHi , Please explain the last difference b/w thread and runnable related to coupling with a more detailed example. Thanks
ReplyDeleteCasino Bonus Codes - December 2021
ReplyDeleteNo deposit bonus casino promotions. We recommend goyangfc 2021 casino poormansguidetocasinogambling.com bonus codes and promos for new players. We casinosites.one also list new casino wooricasinos.info bonuses for December 2021. https://vannienailor4166blog.blogspot.com/