Difference between extends Thread vs Runnable in Java?

In Java there are two means to generate new “thread of execution”. We will divide and label these two approach Thread and Runnable.

Thread Example?

  • Involves the declaration of class to be subclass of the Thread class
  • The subclass overrides the run method of the Thread class
    • The subclass instance can then be invoked
class ThreadExample extends Thread{
   public void run(){
      System.out.println("Thread is running");
   }
   public static void main(String args[]){
      ThreadExample t1=new ThreadExample ();
      t1.start();
   }
}

Runnable Example?

  • This uses a class that implements Runnable interface
  • The class implements the run method
    • An instance of the of the class can then be invoked, passed as an argument when creating Thread, and then started
class RunnableExample implements Runnable{
   public void run(){
      System.out.println("Thread is running for Runnable Implementation");
   }
   public static void main(String args[]){
      RunnableExample runnable=new RunnableExample();
      Thread t1 =new Thread(runnable);
      t1.start();
   }
}

Comparison between Thread and Runnable?

ThreadRunnable
Each thread creates a unique object and get associated with it 🤔Multiple threads share the same object 🤔
Require more memory ❌Requires less memory ✅
The class that extends Thread means that it cannot extend any other classes, as in Java you can only extend to one class ❌The class that implements the Runnable interface allows you to extend another class ✅

Summary

Thread is just class that contains functionality, where Runnable is an interface, that can be describes it like a contract for implementing class. Since Runnable is an interface you need to instantiate a thread to contain it. Comparatively, Thread already contains the ability to create a thread. So in this case Runnable is nice interface when working with the Thread class too. 🤗

📚 Further Reading & Related Topics

If you’re exploring the difference between extends Thread vs. Runnable in Java, these related articles will provide deeper insights:

• Mastering Concurrency in Java: Best Practices for Thread Management – Learn the best practices for managing threads in Java, including when to use Thread vs. Runnable for optimal performance and scalability.

• Understanding Threads in Java: The Difference Between Calling Start and Run Methods – Explore the nuances of starting threads in Java and how Runnable and Thread are invoked for concurrent execution.

Leave a comment

I’m Sean

Welcome to the Scalable Human blog. Just a software engineer writing about algo trading, AI, and books. I learn in public, use AI tools extensively, and share what works. Educational purposes only – not financial advice.

Let’s connect