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.

Runnable_Thread

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 to 3 thread objects to run it.

Whereas by extending Thread class, we need to create 3 different objects of our class to run 3 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

  1. Class MyRunnable implements Runnable{
  2. //We should override only run method of Runnable interface
  3. Public void run(){
  4. //business logic for thread to execute
  5. }
  6. }
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

  1. Class MyThread extends Thread{
  2. Public void run(){
  3. //business logic for thread to execute
  4. }
  5.  
  6. //We have an option to override other important methods of Thread class
  7. }
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.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on