The Thread class and Runnable interface are two ways to create and execute threads in Java. The Thread class represents a thread of execution, while the Runnable interface represents a task that can be executed by a thread. In modern Java development, implementing Runnable is generally preferred because it promotes better object-oriented design and greater flexibility.
Key Points: • Thread is a class, whereas Runnable is a functional interface. • Thread combines both task definition and thread management. • Runnable separates the task from the thread execution mechanism. • Implementing Runnable allows a class to extend another class. • Runnable is the preferred approach in most real-world applications.
Thread Class
When extending the Thread class, the class itself becomes a thread.
Example:
class MyThread extends Thread {
@Override
public void run() {
System.out.println(
"Thread Running");
}
}Characteristics:
• Inherits Thread functionality. • Must override run(). • Cannot extend another class because Java supports single inheritance.
Runnable Interface
When implementing Runnable, the class defines only the task to be executed.
Example:
class MyTask implements Runnable {
@Override
public void run() {
System.out.println(
"Task Running");
}
}
Thread thread =
new Thread(new MyTask());
thread.start();Characteristics:
• Separates task from thread management. • Allows inheritance from another class. • More flexible and reusable.
Example: Suppose an application needs to send emails in the background.
Using Runnable:
• EmailTask defines the email logic. • Thread handles execution.
This separation makes the code cleaner and easier to maintain.
Code Example:
class MyTask implements Runnable {
@Override
public void run() {
System.out.println(
"Task executed by thread");
}
}
public class Demo {
public static void main(String[] args) {
Runnable task =
new MyTask();
Thread thread =
new Thread(task);
thread.start();
}
}Output:
Task executed by thread
Comparison Table
Feature Thread Class Runnable Interface
Type Class Interface
Inheritance Extends Thread Implements Runnable
Multiple Inheritance Not Possible Possible
Task Reusability Limited Better
Code Design Less Flexible More Flexible
Preferred Approach No Yes
Java 8 Lambda Support No Yes
Runnable with Lambda (Java 8+)
Since Runnable is a functional interface, it can be implemented using a lambda expression.
Example:
Thread thread =
new Thread(() -> {
System.out.println(
"Thread Running");
});
thread.start();Benefits:
• Less boilerplate code • Improved readability • Commonly used in modern Java applications
Why Runnable Is Preferred?
1. Better separation of concerns. 2. Supports inheritance from another class. 3. Easier task reuse. 4. Works seamlessly with Executor Framework. 5. More suitable for enterprise applications.
Real-World Usage
Modern applications rarely create threads directly.
Instead, they use:
• Runnable • Callable • ExecutorService • Thread Pools
These approaches provide better scalability and resource management.
Interview Tip: A concise interview answer is:
"The Thread class represents a thread of execution, while the Runnable interface represents a task that can be executed by a thread. Extending Thread tightly couples the task with thread management, whereas implementing Runnable separates the task from execution and allows the class to extend another class. For this reason, Runnable is generally preferred in real-world Java applications."