In Java multithreading, what is the difference between creating a thread by extending Thread and by implementing Runnable, and which is generally preferred?

Difficulty: Medium

Correct Answer: Implementing Runnable is generally preferred because it separates the task from the Thread class and allows the class to extend another superclass

Explanation:


Introduction / Context:
This question builds on the basic knowledge of how to create a thread in Java and focuses on design choices. Understanding the difference between extending Thread and implementing Runnable is important for writing flexible, maintainable multithreaded applications. Interviewers use this question to evaluate your design skills and familiarity with best practices in Java concurrency.


Given Data / Assumptions:

  • We want to run code concurrently using Java threads.
  • The Thread class represents a thread of execution.
  • The Runnable interface represents a unit of work with a run() method.
  • Java supports single inheritance for classes and multiple inheritance for interfaces.


Concept / Approach:
When you extend Thread, your class is tightly bound to the Thread API and cannot extend any other class, because Java allows you to extend only one superclass. The thread logic is directly placed in the Thread subclass. When you implement Runnable, you separate the task (the code in run()) from the mechanism that executes it (the Thread object). This increases flexibility, encourages better design, and allows your class to extend some other meaningful superclass while still providing a Runnable task.


Step-by-Step Solution:
1. Extending Thread: you create class MyThread extends Thread and override the run() method. Then you create an instance MyThread t = new MyThread() and call t.start(). 2. Implementing Runnable: you create class MyTask implements Runnable and implement run(). Then you create a Thread object as Thread t = new Thread(new MyTask()); and call t.start(). 3. In the Runnable approach, the Thread object and the task are separate objects, which makes it easier to reuse the task with different execution strategies or thread pools. 4. Because Java supports only single class inheritance, implementing Runnable allows your MyTask class to extend another class if needed, such as a GUI component or a base domain class. 5. For these reasons, implementing Runnable is generally considered the better design choice in most real projects.


Verification / Alternative check:
You can verify the flexibility advantage by trying to extend another class while also needing thread behavior. With the Thread approach, you are blocked because you cannot extend two classes. With Runnable, you can extend the desired base class and still implement Runnable to describe the task. In addition, modern concurrency utilities such as ExecutorService work naturally with Runnable and Callable, which reinforces this design preference.


Why Other Options Are Wrong:
Option B is wrong because Runnable is explicitly designed to be used with Thread; you pass a Runnable to the Thread constructor. Option C is incorrect; you can pass arguments to a Runnable through its constructor or other methods just like any other object. Option D is false because extending Thread does not automatically synchronize methods; synchronization is controlled by the synchronized keyword and lock management, not by the choice between Thread and Runnable.


Common Pitfalls:
A common pitfall is extending Thread purely out of habit, which restricts your class hierarchy and mixes thread control code with business logic. Another issue is misunderstanding that Runnable is just a functional interface that describes work; it does not itself represent a thread. Finally, developers sometimes forget to call start() when using either approach and mistakenly call run(), which executes synchronously rather than starting a new thread.


Final Answer:
Implementing Runnable is generally preferred because it separates the task from the Thread class and allows the class to extend another superclass.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion