Two ways of creating thread
Two ways of creating thread
Code Example:
Advantages:
Disadvantages:
- Java does not support multiple inheritance with classes, so you can't extend any other class if
you're already extending `Thread`.
Code Example:
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread running via Runnable interface.");
}
}
Advantages:
Disadvantages:
Comparison Table
Feature Thread Class Runnable Interface
Inheritance Extends `Thread` Implements `Runnable`
Multiple Inheritance Not possible Possible
Task Reusability Not reusable Reusable
Separation of Concerns Task + Thread in same class Task logic separated
Better for Simple, short-lived threads Complex, reusable logic
Best Practice
In modern Java development:
- Prefer `Runnable` (or `Callable` for return values) over extending `Thread`, especially in
enterprise or modular code.
- It aligns better with object-oriented principles like abstraction and decoupling.