Difference between Runnable and Thread in Java
In Java we have two options when we want to execute a piece of code on another thread. We can extend Thread
class or implement Runnable
interface and pass our runnable object to some other thread. Runnable is an interface which represents a task that could be executed by either a Thread
or Executor
or some similar means. On the other hand, Thread
is a class which creates a new thread. Implementing the Runnable
interface doesn't create a new thread. Java Docs clearly explains the difference between them.
The
Runnable
interface should be implemented by any class whose instances are intended to be executed by a thread.
This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example,Runnable
is implemented by classThread
. Being active simply means that a thread has been started and has not yet been stopped.In addition,
Runnable
provides the means for a class to be active while not subclassingThread
. A class that implementsRunnable
can run without subclassingThread
by instantiating aThread
instance and passing itself in as the target. In most cases, theRunnable
interface should be used if you are only planning to override therun()
method and no otherThread
methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.
Let’s take an example to make it more clear. We will create a class that will implement Runnable
and then pass it to some Thread
for execution:
class Task implements Runnable{
@Override
public void run (){
System.out.println ("Runnable interface");
}
}class ThreadDemo extends Thread{
@Override
public void run(){
System.out.println( "Thread class ");
}
}class RunnableDemo {
public static void main (String ... args){new ThreadDemo().start();
new Thread (new Task(), "Thread 1").start();
new Thread (new Task(), "Thread 2").start();
}
}
Output for this code will be like this:
Thread class
Runnable interface
Runnable interface
In the above example Task
implements Runnable
and this Task
is executed by two different threads. The Runnable
objects could be shared by many threads. Another way is to extend Thread
class as explained in ThreadDemo
. As ThreadDemo
had already extended Thread class, it can not extend some another class because Java does not support multiple inheritance. There are many reasons to prefer Runnable
over Thread
. Some of them are as below:
- Java does not support multiple inheritance. Hence if your class had already extended some another class, it can not extend Thread class anymore.
- By implementing
Runnable
we can reuse the task to execute it on different threads. Also it could be run by other means likeExecutor
. - Implementing
Runnable
provide Composition and extendingThread
is Inheritance . Try to favor composition over inheritance.
If you have anything to add/correct or doubts, please drop a comment. Thanks for reading. Have a nice day!!!