Implementing Runnable vs extending Thread
As we already understood in previous article that there are 2 ways to create threads in Java
1. By Implementing Runnable interface
2. By Extending Thread class.
But what is the best way to create thread ?
There are few reasons why implementing Runnable interface is more preferable than extending Thread class
Yes, implementing Runnable interface is preferable due to following reasons
1)
If we create a class and implements Runnable interface then this class has still an option to extend any other class
In this way, we can inherit some other behaviours from other class and hence this class will have both Thread behaviour and some additional behaviours.
But,
If we create a class by extending Thread class, then this class can’t extend any other class as multiple inheritance is not supported with respect to class in Java.
In this way, we are restricting our class not to inherit any behaviours from other class.
So, having thread feature without restricting our class for inheritance is always good approach and hence implementing Runnable interface wins over extending Thread class in this case.
2) By extending Thread class, each thread will have a unique object associated with it, whereas implementing Runnable interface, many threads can share the same runnable instance.
Example:
If we want to create 3
threads and run our logic then
By implementing Runnable interface, we need to create only one runnable instance and pass it to3
thread objects to run it. Whereas by extending Thread class, we need to create3
different objects of our class to run3
threads.
In this way, for every one thread creation, we need to create our Thread class object and this will consume more memory.
3) If we create a class by implementing Runnable interface then it means we are not modifying the actual thread behaviour, instead we are actually giving this to Thread to run through composition
In simple words, we will not
extend Thread class and hence we can’t override any behaviour of Thread class
We will override only “run()” method of Runnable interface and we provide this to Thread class object to run it.
Example:
Implementing Runnable interface
- Class MyRunnable implements Runnable{
- //We should override only run method of Runnable interface
- Public void run(){
- //business logic for thread to execute
- }
- }
Class MyRunnable implements Runnable{ //We should override only run method of Runnable interface Public void run(){ //business logic for thread to execute } }
In the above case, we are not allowed to override any Thread class behaviour as we are not extending it.
Extending Thread class
- Class MyThread extends Thread{
- Public void run(){
- //business logic for thread to execute
- }
- //We have an option to override other important methods of Thread class
- }
Class MyThread extends Thread{ Public void run(){ //business logic for thread to execute } //We have an option to override other important methods of Thread class }
In the above case, we have an option to override Thread class behaviours which is not safe as we should not be able to override any other methods of Thread class except “run()” method.
Note:
Considering all the above points, Our Strong recommendation and best approach to create threads is by implementing Runnable interface.