Sunday, October 24, 2021

7 Difference between extends Thread vs implements Runnable in Java [Answer]

Hello guys, the difference between Thread vs Runnable in Java is a common Java multithreading interview question that is often asked by junior Java developers with 2 to 4 years of experience. If you are doing Java programming then you may know that Java provides multithreading to parallelize the execution of tasks (code) and you need multiple threads to run multiple things in parallel like downloading a file in the background and showing the progress bar at the 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 most interviewers love 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 extending Thread and implementing Runnable in Java.

Btw, if you are a complete beginner to Java's multithreading and concurrency functionalities and APIs, I highly recommend you to join a good multithreading course like Multithreading and Parallel Computing in Java from Udemy. It's a great course to learn the basics which go a long way in doing well on interviews and becoming a better Java developer. 





Difference between 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, the first one to download the file and the second one to show the progress bar. Even though Java provides Thread class and multi-threading it's the programmer's responsibility to create and manage threads. 

Though JDK 5 also provides an Executor framework that can handle the creation and management of threads as a Java developer, you should know how to create, start, stop, and pause threads by yourself.

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

1. Class Extension Limitation

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 loses that option and it cannot extend another class, but if you implement Runnable then your Thread class can still extend another class like Canvas. 

It's a common pattern in Java GUI programming that your class extends Canvas and implements Runnable, EventListener, etc. 

If you want to learn more about essential Java features like class vs interface, Inheritance, Encapsulation, and even thread basics, I highly recommend you to join a comprehensive Java course like The Complete Java Masterclass on Udemy, which is also the most up-to-date Java course online. 





2. Better Encapsulation

The second difference between extending Thread and implementing 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. Coupling

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

4. Overhead

 Another difference between Thread and Runnable comes from the fact that you are extending Thread class just for the 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 the run() method for parallel execution then use Runnable instead of extending Thread class.

5. OOP

The fifth difference between extending Thread and implementing Runnable also comes from the 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 the Thread class.



6. Maintenance

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


7. Best Practice

Last but not least difference between extending Thread and implementing 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 the Executor framework. 

And, if you want to go to the next level, you can further see the Java Multithreading, Concurrency, and Performance Optimization course to learn more about performance optimization and writing solid concurrent code in Java

best course to learn multithreaing in Java




Conclusion

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 differences 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 a Runnable interface to define a Task that can be executed with threads.

 Other Java Multithreading and Concurrency Articles you may like
  • 5 Courses to Learn Java Multithreading in-depth (courses)
  • Top 5 Books to Master Concurrency in Java (books)
  • Difference between volatile, synchronized, and atomic variable in Java (answer)
  • 10 Java Multithreading and Concurrency Best Practices (article)
  • How to pause a Thread in Java? (solution)
  • Top 50 Multithreading and Concurrency Questions in Java (questions)
  • Difference between CyclicBarrier and CountDownLatch in Java? (answer)
  • How to avoid deadlock in Java? (answer)
  • Difference between Executor and ExecutorService in Java? (answer)
  • Understanding the flow of data and code in Java program (answer)
  • How to join two threads in Java? (answer)
  • How to do inter-thread communication in Java using wait-notify? (answer)
  • How to stop a Thread in Java? (answer)
  • Difference between ForkJoinPool and Executor Framework in Java(answer)
  • 5 Essential Skills to Crack Java Interviews (skills)
  • What is Happens Before in Java Concurrency? (answer)

Thanks for reading this article so far. If you like these differences between Thread and Runnable class then please share them with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are serious to improve your Java Multithreading and Concurrency Skills but looking for a free course to start with then I also, suggest you check out this awesome free Java Multithreading course on Udemy. 

2 comments:

  1. Hi ,

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

    Thanks
    Arun

    ReplyDelete
  2. Years ago, I ran into problems with Android using implements Runnable and had to use extends Thread to get it working right. Wish I could remember the context.

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.