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.
Feature | Runnable | Thread |
---|---|---|
Implementation | This is an Interface | This is a Class |
Inheritance | The child class can extend another class | The child class cannot extend another class |
Code Reusability | A single instance can be shared among multiple threads | Each thread needs a new instance |
Resource Sharing | Allows sharing of resources among multiple threads | More difficult to share resources |
Memory Overhead | Lower memory overhead as no separate object is created for the thread | Higher 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 !!
Comments