Java – Thread class v/s Runnable interface

In this article, we will discuss difference between extending a Thread class and implementing a Runnable interface for creating a new Thread

1. Thread class :

  • Java allows maximum of one class to be extended
  • When new Thread is created extending Thread class then it become difficult to extend another class even when it is required for business purpose
  • Also, whenever a new Thread is created using this approach a unique object is created every time
  • Thread class implements Runnable interface overriding run() method providing dummy implementation

Thread.java

public class Thread implements Runnable {

	// methods and constructors

	@Override
	public void run() {
		if (target != null) {
			target.run();
		}
	}
}

2. Runnable interface :

  • Creating a new Thread by implementing Runnable interface allows to extend another class if required
  • Also, whenever a new Thread is created using this approach it shares the same object for multiple Threads
  • Runnable interface has only one abstract method – run()
  • Whenever Runnable interface is implemented by any class then abstract run() method need to be overridden providing implementation

Runnable.java

@FunctionalInterface
public interface Runnable {

    public abstract void run();
}

3. Thread class v/s Runnable interface :

Both Thread class and Runnable interface can be used to create or spawn a new Thread by overriding run() method but they’re 2 different approaches,

Sr. No.Thread classRunnable interface
1.Thread class implements Runnable interface and overrides abstract run() method providing dummy implementationRunnable is a Functional interface with single abstract methodrun()
2.If new Thread is created by extending Thread class then Java doesn’t allows to extend any other class even when it is utmost requiredIf new Thread is created by implementing Runnable interface then Java allows to extend other class (strictly only one) when it is required
3.New Thread created extending Thread class doesn’t forces to override/implement run() method as it has dummy implementationNew Thread created implementing Runnable interface forces to override/implement abstract methodrun()
4.New Thread created using this approach (extending Thread class) always creates unique object every timeNew Thread created using this approach (implementing Runnable class) shares same object for multiple Threads
5.Fully qualified name of Thread class is java.lang.ThreadFully qualified name of Runnable interface is java.lang.Runnable
6.Introduced in JDK 1.0 versionIntroduced in JDK 1.0 version

Related Articles :

  • Java – Introduction to Threads
  • Java – Different ways to create/spawn a Thread
  • Java – Thread class v/s Runnable interface
  • Java – Thread Scheduler
  • Java – Important methods and constructors of a Thread class
  • Java – Difference between start() and run() methods of Thread class
  • Java – Importance of start() method in Thread class
  • Java – Overloading run() method of Thread class
  • Java – Effects of not overriding run() method of Thread class
  • Java – Setting and getting name of a Thread

References :

Happy Coding !!
Happy Learning !!

Java – Thread Scheduler
Java – Different ways to create/spawn a Thread