Differences between implementing Runnable interface and extending Thread class in java


There have been many debates over this topic many developers tend to confuse when it comes to implementing Runnable and extending Thread. So, i’ll be giving you clear picture of what should go for and why you to go for it.

Differences between implementing Runnable interface and extending Thread class-

  1. Multiple inheritance in not allowed in java : When we implement Runnable interface we can extend another class as well, but if we extend Thread class we cannot extend any other class because java does not allow multiple inheritance. So, same work is done by implementing Runnable and extending Thread but in case of implementing Runnable we are still left with option of extending some other class. So, it’s better to implement Runnable.



  1. Thread safety : When we implement Runnable interface, same object is shared amongst multiple threads, but when we extend Thread class each and every thread gets associated with new object.

  1. Inheritance (Implementing Runnable is lightweight operation) : When we extend Thread unnecessary all Thread class features are inherited, but when we implement Runnable interface no extra feature are inherited, as Runnable only consists only of one abstract method i.e. run() method.  So, implementing Runnable is lightweight operation.

  1. Coding to interface : Even java recommends coding to interface. So, we must implement Runnable rather than extending thread. Also, Thread class implements Runnable interface.

  1. Don’t extend unless you wanna modify fundamental behaviour of class, Runnable interface has only one abstract method i.e. run()  : We must extend Thread only when you are looking to modify run() and other methods as well. If you are simply looking to modify only the run() method implementing Runnable is the best option (Runnable interface has only one abstract method i.e. run() ). We must not extend Thread class unless we're looking to modify fundamental behaviour of Thread class.

  1. Flexibility in code when we implement Runnable : When we extend Thread first a fall all thread features are inherited and our class becomes direct subclass of Thread , so whatever action we are doing is in Thread class. But, when we implement Runnable we create a new thread and pass runnable object as parameter, we could pass runnable object to executorService & much more. So, we have more options when we implement Runnable and our code becomes more flexible.

  1. ExecutorService : If we implement Runnable, we can start multiple thread created on runnable object  with ExecutorService (because we can start Runnable object with new threads), but not in the case when we extend Thread (because thread can be started only once).


Conclusion : I’ll recommend you to go for implementing Runnable rather than extending Thread class.



/** JavaMadeSoEasy.com */


RELATED LINKS>

Thread basics >

What is thread in java

Thread states/ Thread life cycle in java

Difference between Process and Thread in java

Implementing Threads in java by implementing Runnable interface and extending Thread class

Threads implement their own stack - demonstration using program and diagram

Differences between implementing Runnable interface and extending Thread class

Thread behaviour is unpredictable

When threads are not lightweight process in java, in fact they become heavy weight process



No comments:

Post a Comment