HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Multi-threading / Difference between Runnable vs Thread in Java

Difference between Runnable vs Thread in Java

In java language, as we all know that there are two ways to create threads. One using Runnable interface and another by extending Thread class. Let’s identify the differences between both ways i.e extends thread and implements runnable.

1. Create Thread using Runnable Interface vs Thread class

Let’s quickly check the java code of usage of both techniques.

1.1. Runnable interface

Java program to create thread by implementing Runnable interface.

public class DemoRunnable implements Runnable {
    public void run() {
    	//Code
    }
}

//start new thread with a "new Thread(new demoRunnable()).start()" call

1.2. Thread class

Java program to create thread by extending Thread class.

public class DemoThread extends Thread {
    public DemoThread() {
    	super("DemoThread");
    }
    public void run() {
    	//Code
    }
}
//start new thread with a "new demoThread().start()" call

2. Difference between Runnable vs Thread

There has been a good amount of debate on which is better way. Well, I also tried to find out and below is my learning.

  1. Implementing Runnable is the preferred way to do it. Here, you’re not really specializing or modifying the thread’s behavior. You’re just giving the thread something to run. That means composition is the better way to go.
  2. Java only supports single inheritance, so you can only extend one class.
  3. Instantiating an interface gives a cleaner separation between your code and the implementation of threads.
  4. Implementing Runnable makes your class more flexible. If you extend Thread then the action you’re doing is always going to be in a thread. However, if you implement Runnable it doesn’t have to be. You can run it in a thread, or pass it to some kind of executor service, or just pass it around as a task within a single threaded application.
  5. If you are working on JDK 4 or lesser, then there is bug :

    http://bugs.java.com/bugdatabase/view_bug.do;jsessionid=5869e03fee226ffffffffc40d4fa881a86e3:WuuT?bug_id=4533087

    It’s fixed in Java 1.5 but Sun doesn’t intend to fix it in 1.4.

    The issue is that at construction time, a Thread is added to a list of references in an internal thread table. It won’t get removed from that list until its start() method has completed. As long as that reference is there, it won’t get garbage collected.

That’s all about differences between Runnable interface and Thread class in java. If you know something more, please put that in comments section and I will include in post content.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. steven

    February 12, 2020

    At point 4, how can a task(non-run metho) inside Thread extending class, still need to run in thread only. It can still be executed within a main thread. Could u pls elaborate?

  2. Iqbal sarkar

    January 24, 2020

    Thread creating using Runable is bettter in terms of lamda expression and Functional interface

  3. Soumyaranjan

    April 26, 2019

    By implementing Runnable interface, we can only override the run() method. But when we extend Thread class, we can use many methods based on our requirements. If we want to make our class as a thread, we can always go for extending the Thread class rather than implementing Runnable interface.

    • Rahul Kashyap

      June 20, 2019

      Hello Sowmya rajan,

      As per my understanding, Thread is a curse in java. If you create multiple threads then there would be a chance of memory overhead and hence the performance of the app will go down.

  4. Akash

    September 19, 2017

    why we extends a thread class and what are the advantage of it

  5. Ashish

    October 26, 2016

    Well Explained. Thanks to Lokesh !

    • Naveen

      January 24, 2017

      Good points on topic

      Thanks lokesh..

  6. Gopi

    January 8, 2015

    As per my knowledge, the advantage is – when there are multiple threads then, memory usage would be more in case of extends Thread. Because, each of your threads contains unique object associated with it. Where as, memory usage would be less in case of implements Runnable. Because, many threads can share the same runnable instance.

    • Lokesh Gupta

      January 9, 2015

      You are right Gopi. Point 5 state the same thought as you. Key thing is that it “CAN” share (as you also said); not always.

  7. sreenath

    September 3, 2014

    Awesome explanation !!
    Small correction in point 4

    It should be ” implements Runnable” Not “extend Runnable”

    • pratik

      September 16, 2014

      i want what the main difference in the thread & and Runnable interface?

  8. Manoj

    December 24, 2013

    5) By extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same runnable instance. – This statement seems wrong to me.

    Many threads can share the same Thread object, same as Runnable instance.
    For example,
    public class A1 implements Runnable {….}
    public class B1 extends Thread {….}

    public class ABMain {
    public static void main( String[] args ) {
    A1 a = new A1(); //Create runnable instance
    B1 b = new B1(); // Create Thread instance

    new Thread( a ).start(); //Starts runnable instance

    new Thread( b ).start(); //Starts Thread instance.

    new Thread( a ).start(); // 2nd thread sharing same runnable instance A1
    new Thread( b ).start(); // 2nd thread sharing same Thread instance.

    }
    }

    Difference between Thread and Runnable I found is, Thread class provides apis to configure Thread instance based on your need.
    If you extends Thread class, then
    1. you will have flexibility to set priority to Thread instance,
    2. you can create daemon thread,
    3. you can check if thread is terminated, alive etc.

    • Manoj

      December 24, 2013

      There are apis available in Thread class which is not avaialbe in Runnable. Those apis helps developers to configure Thread properties.

    • Lokesh Gupta

      December 24, 2013

      I modified your program a little bit, to correct the picture of usage of thread and runnable.

      class A1 implements Runnable {
      private int counter = 0;
      @Override
      public void run() {
      counter++;
      System.out.println("Running A1 -> " + counter);
      }
      }

      class B1 extends Thread {
      private int counter = 0;
      @Override
      public void run() {
      counter++;
      System.out.println("Running B1 -> " + counter);
      }
      }

      public class ABMain {
      public static void main(String[] args) throws InterruptedException {
      A1 a = new A1(); // Create runnable instance
      B1 b = new B1(); // Create Thread instance

      new Thread(a).start();
      Thread.sleep(1000);
      new Thread(a).start();
      Thread.sleep(1000);
      new Thread(a).start();
      Thread.sleep(1000);

      new B1().start();
      Thread.sleep(1000);
      new B1().start();
      Thread.sleep(1000);
      new B1().start();
      }
      }

      Output:

      1
      2
      3

      1
      1
      1

      1) A1 a = new A1(); does not make a thread. It’s just another class with no extra behavior. If you call A1.run() then it is not new thread. And A1.start() is not available to this class.
      2) Only way to create start a thread in java is calling it’s start method.
      3) If you look at constructor of Thread class, no constructor takes parameter of Thread class itself. And we know that Thread implements Runnable, effectively any call such as “new Thread(a).start();” is starting a thread with runnable mechanism.
      4) Correct way to start thread (“using extend”) is calling it’s start method only directly.
      e.g. new B1().start();
      5) So effectively, in both techniques in your code, you are doing the same thing i.e. implementing runnable.

      • satish Kumar Singh

        April 3, 2014

        It’s correct post.

      • Dhruv

        November 27, 2014

        By extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same runnable instance.

        The statement is alright but what is the advantage we gain here ?

      • Ashwini

        July 10, 2018

        The code is misleading. It doesn’t match the intent. It’s true that Thread class itself implements Runnable interface, so any instance of a class extending the Thread class can be passed in to creating new thread.

        ExtendsThread et = new ExtendsThread(); 
        Thread tc1 = new Thread(et); tc1.start(); 
        Thread tc2 = new Thread(et); tc2.start();

        These two threads share the same instance.

        • Lokesh Gupta

          July 10, 2018

          You have a point. Removing the statement from above post this until I find a reason to include.

  9. Sanchita Vijayvargiya

    November 13, 2013

    Thank you for the post. It’s very helpful.

Comments are closed on this article!

Search Tutorials

Java Concurrency Tutorial

  • Java Concurrency – Introduction
  • Concurrency Evolution
  • Thread Safety
  • Concurrency vs. Parallelism
  • Compare and Swap [CAS]
  • synchronized keyword
  • Object vs. Class Level Locking
  • Runnable vs. Thread
  • wait(), notify() and notifyAll()
  • Yield() vs. Join()
  • Sleep() vs. Wait()
  • Lock vs. Monitor
  • Callable + Future
  • UncaughtExceptionHandler
  • Throttling Task Submission
  • Executor Best Practices
  • Inter-thread Communication
  • Write and Resolve Deadlock

Java Concurrency Utilities

  • AtomicInteger
  • Lock
  • ThreadFactory
  • ThreadLocal
  • ExecutorService
  • ThreadPoolExecutor
  • FixedSizeThreadPoolExecutor
  • ScheduledThreadPoolExecutor
  • Semaphore
  • Binary Semaphore
  • BlockingQueue
  • DelayQueue
  • ConcurrentLinkedDeque
  • CountDownLatch
  • ForkJoinPool

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces