Difference Between Runnable and Thread in Java
1. Introduction
In Java, multithreading can be achieved in two main ways:
By extending the Thread class
By implementing the Runnable interface
Both are used to create threads, but they differ in design, flexibility, and usage.
2. Thread Class
Explanation
Threadis a class in Java.To create a thread, you extend this class and override the
run()method.Java does not support multiple inheritance, so extending Thread limits flexibility.
Example
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
}
}
public class ThreadExample {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
}
}
Explanation of Code
MyThread extends Thread→ Inheriting Thread classrun()→ Defines the taskstart()→ Creates a new thread and internally callsrun()
3. Runnable Interface
Explanation
Runnableis an interface.You implement it and pass the object to a Thread.
Allows multiple inheritance (via interfaces) → more flexible design.
Example
class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable is running...");
}
}
public class RunnableExample {
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
}
}
Explanation of Code
implements Runnable→ Define task separatelyPass object to
Threadstart()executesrun()
4. Key Differences Table
5. Why Runnable is Preferred?
Explanation
Java supports single inheritance only.
If you extend Thread, you cannot extend any other class.
Runnable separates:
Task (Runnable)
Thread (Thread class)
This leads to better design and maintainability.
6. Real-Time Example
class Task implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
public class Main {
public static void main(String[] args) {
Task task = new Task();
Thread t1 = new Thread(task);
Thread t2 = new Thread(task);
t1.start();
t2.start();
}
}
Explanation
Same task shared by multiple threads
Promotes resource sharing and efficiency
7. Summary
Thread → Simpler but less flexible
Runnable → Flexible and widely used
Runnable is recommended in real-world applications
Java Full Stack Developer Roadmap
To master multithreading and advanced Java concepts:
👉 https://www.ashokit.in/java-full-stack-developer-roadmap
Promotional Content
Want to master Java Multithreading concepts like Runnable and Thread?

Comments
Post a Comment