Runnable vs Thread in Java

Multithreading is a powerful tool that can improve the performance and responsiveness of our Java applications. However, when it comes to implementing multithreading, we're faced with a choice: should we extend the Thread class or should we implement the Runnable interface? In this blog post, we'll dive into the details of both approaches and compare them side-by-side to help you make the right decision.

Overview of Thread and Runnable

Extending Thread class

When you extend the Thread class, you're creating a new class that inherits all the properties and methods of the Thread class, including the run() method, which you can override with the code you want the thread to execute:

public class ThreadExample extends Thread {

    // run() method contains the code that is executed by the thread.
    @Override
    public void run() {
        System.out.println("Inside : " + Thread.currentThread().getName());
    }
}

After creating the class, you can instantiate and start the thread like so:

        Thread thread = new ThreadExample();
        thread.start();

Implementing Runnable interface

On the other hand, the Runnable interface provides a single method, run(), that you must implement to define the code the thread will execute:

public class RunnableExample implements Runnable {

    @Override
    public void run() {
        System.out.println("Inside : " + Thread.currentThread().getName());
    }
}

After implementing the Runnable interface, you can create a runnable instance and pass it to the Thread instance:

        Runnable runnable = new RunnableExample();
        Thread thread = new Thread(runnable);
        thread.start();

Comparison

Let's compare these two options based on some key factors: 

1. Inheritance: 

Java doesn't support multiple inheritances of classes. So, if you choose to extend the Thread class, your class can no longer extend to any other class. However, if you implement the Runnable interface, your class is still free to extend to another class. So, if you need to inherit from another class, Runnable is the way to go. 

2. Code Separation: 

The Runnable interface separates the task to be performed from the Thread object that performs it while extending Thread couples them together. The Runnable approach promotes cleaner, more modular code, adhering to the Single Responsibility Principle. 

3. Object Sharing: 

If you need to share an object between multiple threads, it's easier to do so with Runnable. When multiple threads execute the same Runnable object, they can easily share the object state. But with Thread, each thread has its own object instance. 

4. Overhead: 

Creating a new thread using the Thread class is an expensive operation that uses significant system resources. If your application needs to create many threads, consider using Runnable with a thread pool, which reuses a fixed number of threads. 

5. Use Lambda Expressions: 

Since Java 8, Runnable can be used with lambda expressions for a more concise and readable syntax, a feature that is not possible with Thread.
        Runnable runnable = () -> {
            System.out.println("Inside : " + Thread.currentThread().getName());
        };

        System.out.println("Creating Thread...");
        Thread thread = new Thread(runnable);

        System.out.println("Starting Thread...");
        thread.start();

Runnable or Thread, Which one to use?

The first method, where you create a thread by extending from the Thread class is very limited because once you extend your class from Thread, you cannot extend from any other class since Java doesn’t allow multiple inheritance. Also, If you follow good design practice, Inheritance is meant for extending the functionality of the parent class, but when you create a thread, you don’t extend the functionality of the Thread class, you merely provide the implementation of a run() method. 

So, In general, You should always use a Runnable object to create a thread. This method is more flexible. It allows your class to extend from any other class. Also, you can use anonymous class syntax and Java 8’s lambda expression with Runnable to make your code more concise.

That wraps up our comparison. We hope it will help you make a more informed decision when implementing threads in your Java applications. Happy coding!

Comments