A thread can be created in Java by extending from Thread class or implementing the Runnable interface. While runnable interface has only one method named “run”, Thread class has a whole lot of methods defined in it. Both of these can be used to create threads and then start them. But when it comes to which one out of Thread class or Runnable interface to choose, there seems some confusion among developers.

confused

I have seen many beginners and intermediate level developers don’t even know the purpose of Thread class. In interviews, it is a very common question. The best part is when the interviewer asks why the need for two ways to create a thread. When thread can be created using the Runnable interface then what purpose does Thread class solve. Why do we need the Thread class in Java?

In this tutorial, we shall learn the difference between Thread class and Runnable interface in terms which one to use in which scenario. While Thread class extension doesn’t allow for extending from any other classes, one can always implement or extend classes when implementing the Runnable interface. To start with, let us see the code for creating a thread using Thread class and then using the Runnable interface:

Thread class:

When extending from the Thread class, the custom class inherits all the methods and hence there is no need to create a separate instance of Thread class as with Runnable interface. It is important to mention here that the Thread class internally implements the Runnable interface.

package com.example;

public class Test extends Thread{
 
	public void run() {
		System.out.println("Serving run method");
	}
	
	public static void main(String[] args) {
		Test t = new Test();
		t.start();
	}
}
  • Thread class implements Runnable interface.
  • Thread class contains the start method required to start the thread.
  • We always need a Thread class instance to create a thread.
  • If our custom class extends from Thread class then we need not create a separate Thread class instance and can invoke start method on the object of our custom class.
  • Thread class also adds some helper methods like sleep and yield.

Runnable Interface:

When using the Runnable interface, we need to create two objects viz. the current custom class object and one Thread class instance. When creating the Thread class instance, we need to pass the custom class instance to the constructor.

package com.example;

public class Test implements Runnable{
 
	public void run() {
		System.out.println("Serving run method");
	}
	
	public static void main(String[] args) {
		Test t = new Test();
		Thread th = new Thread(t);
		th.start();
	}
}

Which one to choose:

When there is more strong relationship between two classes then using the Runnable interface for creating thread makes more sense. This is because of the fact that Java doesn’t allow multiple inheritance for a single class. The code for using the Runnable interface is shown above. It is very simple to use the Runnable interface. The only point to note is that we need to override the “public void run()” method.

But when there is no other inheritance relationship between the custom thread class and any other class then using Thread class is a good option. Another common requirement for using the Thread class for creating a thread is when the sleep, yield and join methods are to be used for thread management. Note that if you extend from Thread class, then you get tied to it in future versions of the applications too. Thus is it is important to visualize if there would be any kind of inheritance relationship for our thread class before going for Thread class code shown above.

Create thread using Runnable interface or Thread class in Java admin Core Java
A thread can be created in Java by extending from Thread class or implementing the Runnable interface. While runnable interface has only one method named 'run', Thread class has a whole lot of methods defined in it. Both of these can be used to create threads and then start...
A thread can be created in Java by extending from Thread class or implementing the Runnable interface. While runnable interface has only one method named "run", Thread class has a whole lot of methods defined in it. Both of these can be used to create threads and then start them. But when it comes to which one out of Thread class or Runnable interface to choose, there seems some confusion among developers. <img src="http://www.javaexperience.com/wp-content/uploads/2013/01/confused.jpg" alt="confused" width="464" height="223" class="alignnone size-full wp-image-2245" /> I have seen many beginners and intermediate level developers don't even know the purpose of Thread class. In <a href="http://www.javaexperience.com/java-test-for-developers/" title="A java test for developers">interviews</a>, it is a very common question. The best part is when the interviewer asks why the need for two ways to create a thread. When thread can be created using the Runnable interface then what purpose does Thread class solve. Why do we need the Thread class in Java? In this tutorial, we shall learn the difference between Thread class and Runnable interface in terms which one to use in which scenario. While Thread class extension doesn't allow for extending from any other classes, one can always implement or extend classes when implementing the Runnable interface. To start with, let us see the code for creating a thread using Thread class and then using the Runnable interface: <h3>Thread class:</h3> When extending from the Thread class, the custom class inherits all the methods and hence there is no need to create a separate instance of Thread class as with Runnable interface. It is important to mention here that the Thread class internally implements the Runnable interface. 1 <ul> <li>Thread class implements Runnable interface.</li> <li>Thread class contains the start method required to start the thread.</li> <li>We always need a Thread class instance to create a thread.</li> <li>If our custom class extends from Thread class then we need not create a separate Thread class instance and can invoke start method on the object of our custom class.</li> <li>Thread class also adds some helper methods like sleep and yield.</li> </ul> <h3>Runnable Interface:</h3> When using the Runnable <a href="http://www.javaexperience.com/java-marker-interfaces/" title="What are Marker or Tag Interfaces in Java">interface</a>, we need to create two objects viz. the current custom class object and one Thread class instance. When creating the Thread class instance, we need to pass the custom class instance to the constructor. 1 <h3>Which one to choose:</h3> When there is more strong relationship between two classes then using the Runnable interface for creating thread makes more sense. This is because of the fact that Java doesn't allow <a href="http://www.javaexperience.com/multiple-inheritance-in-java/" title="Multiple inheritance in Java">multiple inheritance</a> for a single class. The code for using the Runnable interface is shown above. It is very simple to use the Runnable interface. The only point to note is that we need to override the "public void run()" method. But when there is no other inheritance relationship between the custom thread class and any other class then using Thread class is a good option. Another common requirement for using the Thread class for creating a thread is when the sleep, yield and join methods are to be used for thread management. Note that if you extend from Thread class, then you get tied to it in future versions of the applications too. Thus is it is important to visualize if there would be any kind of inheritance relationship for our thread class before going for Thread class code shown above.
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.