Difference between Runnable vs Thread in Java

In Java Concurrency, until we have a particular reason, it is always recommended to implement the Runnable interface to create new threads.

Runnable vs Thread

In Java, there are two ways to create threads i.e. implementing Runnable interface and extending Thread class. In this Java concurrency tutorial, we will identify the differences between both ways i.e. extends thread Vs. implements runnable.

In general, until we have a particular reason, it is always recommended to implement the Runnable interface to create new threads. This way, the child class is free to extend another class, if needed.

We should extend the Thread class only if we want to override or extend the behavior of the Thread class itself, and it is not recommended at all.

1. Thread Class

The Thread class represents a thread of execution in a Java program. We extend the Thread class in a child class and override the run() method.

public class MyThread extends Thread {

    @Override
    public void run() {
        System.out.println("Thread is running");
    }
}

To run the thread, we call its start() method.

MyThread thread = new MyThread();
thread.start();

The Thread class itself implements the Runnable interface.

2. Runnable Interface

A class should implement the Runnable interface if instances are intended to be executed by a thread. The code to be executed is written in the run() method.

public class MyRunnable implements Runnable {

    @Override
    public void run() {
        System.out.println("Runnable is running");
    }
}

To run the runnable code, we create a new Thread object and pass the runnable as a constructor argument. After this, we can use the Thread.start() method to run the executable code.

MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();

3. Difference between Runnable vs. Thread

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

FeatureRunnableThread
ImplementationThis is an InterfaceThis is a Class
InheritanceThe child class can extend another classThe child class cannot extend another class
Code ReusabilityA single instance can be shared among multiple threadsEach thread needs a new instance
Resource SharingAllows sharing of resources among multiple threadsMore difficult to share resources
Memory OverheadLower memory overhead as no separate object is created for the threadHigher memory overhead as each thread object has its own memory

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, pass it to some kind of executor service, or just pass it around as a task within a single-threaded application.

4. Conclusion

Implementing Runnable is the preferred way to do it. Here, you’re not 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.

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

Happy Learning !!

Weekly Newsletter

Stay Up-to-Date with Our Weekly Updates. Right into Your Inbox.

Comments

Subscribe
Notify of
20 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.