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

  • Thread is 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 class

  • run() → Defines the task

  • start() → Creates a new thread and internally calls run()




3. Runnable Interface

Explanation

  • Runnable is 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 separately

  • Pass object to Thread

  • start() executes run()


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?

Top Core JAVA Online Training in 2026

Comments

Popular posts from this blog

How Does HashMap Work Internally in Java?

What is Docker Used for in Java Applications?

Java Future Interface: Complete Practical Guide with Real-Time Examples for Modern Developers (2026)