7 differences between extends Thread and implements Runnable in Java

Java provides multithreading to parallelize execution of tasks (code) and you need threads to run multiple things in parallel e.g. download a file in the background and show the progress bar at front-end. There are two ways to create a Thread in Java, first by extending java.lang.Thread class and second by implementing the java.lang.Runnable interface. Since interviewer loves comparison based questions, what is the  difference between extending thread and implementing Runnable is also a popular Java thread question. In this article, I'll tell you how to answer this question by explaining the difference between extending the Thread class and implementing the Runnable interface, and which one is the better way to create threads In Java. Both approaches have their pros and cons and there is a situation when extending Thread is logical but in most cases implementing Runnable is the better option. Let's see the difference between extends Thread and implements Runnable in Java.



Extends Thread vs implements Runnable

In order to download a file in the background and show the progress bar in GUI you need two threads, first one to download the file and second one to show the progress bar. Even though Java provides Thread class and multi-threading it's programmer's responsibility to create and manage threads. Though, JDK 5 also provides Executor framework which can handle creation and management of threads but as a Java developer, you should know how to create, start, stop, and pause thread by yourself.

As I said, there are two main ways to create a thread in Java, by extending Thread class and overriding run() method or by implementing Runnable interface and overriding run() method, let's see the advantages and disadvantage and differences between these two approaches.


1) The first and most important difference between extending Thread and implementing Runnable comes from the fact that a class can only extend one class in Java. So if you extend the Thread class then your class lose that option and it cannot extend another class, but if you implement Runnable then your Thread class can still extend another class e.g. Canvas. It's a common pattern in Java GUI programming that your class extends Canvas and implements Runnable, EventListener etc.


2) The second difference between extends Thread and implements Runnable is that using the Runnable instance to encapsulate the code which should run in parallel provides better reusability. You can pass that Runnable to any other thread or thread pool.


3) The third difference comes from OOP perspective. In case, you implement Runnable, both Task and Executor ( a thread which execute the task) are loosely coupled but if you extend Thread then they are tightly coupled.


4) Another difference between Thread and Runnable comes from the fact that you are extending Thread class just for run() method but you will get overhead of all other methods which come from Thread class. So, if your goal is to just write some code in run() method for parallel execution then use Runnable instead of extending Thread class.


5) The fifth difference between extending Thread and implementing Runnable also comes from OOP perspective. In Object oriented programming you extend a class to enhance it, to put some new features on it. So, if you just want to reuse the run() method, then stick with implementing the Runnable interface rather than extending Thread class.


6) It's easier to maintain code encapsulated in Runnable interface because you only need to make the change in one place but if that code is scattered around multiple Thread class, you need to make the change at multiple places.


7) Last but not the least difference between extends Thread and implements Runnable is that it's good coding practice to use Runnable for the specifying task as you can reuse it on Thread as well as on Executor framework.



Summary

If you extend Thread then you can't extend another class, you will tightly couple the task and runner and maintenance of the code will be tough, but if you implement Runnable then you can still extend another class, task and runner will be loosely coupled and maintenance of code will be easier.
Here is a nice table of difference between extends Thread and implements Runnable in Java:

Difference between extending thread and implementing Runnable in Java


That's all about the difference between extends Thread and implements Runnable in Java. You can clearly see that implementing Runnable is better than Thread in most of the cases except one where you quickly want to test something. Stick with best practice and encapsulate the code with Runnable interface to define a Task which can be executed with threads.


Further Reading
  • The difference between a Thread and a Process in Java? (answer)
  • How to stop a Thread in Java? (answer)
  • How to join two threads in Java? (answer)
  • How to pause a Thread in Java? (solution)
  • Java Threads By Scott Oaks and Henry Wong (book)
  • Java Concurrency in Practice by Brian Goetz (book)



1 comment:

  1. Hi ,

    May i know how thread is tightly coupled and runnable is loosely coupled give me a example for that

    Thanks
    Arun

    ReplyDelete