Awesome Answer for Extending Thread Class vs Implementing Runnable interface in Java

Very nice and concise answer with examples for technical interview question what difference between Extending Thread class and Implementing Runnable Interface in Java.

Before answering the differences lets have a brief note and tips that will help you answer the best.

Examples of creating threads in java using extending Thread class and Runnable Interface

NOTE:

Many people answer this question that if we extend thread class, we are losing a chance to extend another class. But if we use runnable interface, we save a chance to extend another class as multiple class cannot be extended. Java does not support multiple inheritance using classes but interfaces in java.

That’s it.

Even though above difference is correct, it is not an impressive answer. And if you are an experience, then it cannot be impressive at all.

Tips: Answer for difference between extending thread class and Runnable interface in java should contains clear differences with examples. So, an interviewer can understand that you are very clear in your concept. However, as soon as he understands that you have the clear difference, he can switch to other questions in between.

ANSWER:

There are differences between extending thread class and implementing Runnable interface regarding following points.

  1. Threads objects
  2. Memory consumption of Threads
  3. Threads in case of Multiple inheritance
  4. Overriding threads methods facility

1) Thread Objects

When we extend Thread class, each thread creates unique objects of thread class. For example, if you create 5 threads, we have 5 different objects in the memory.

When we implement Runnable interface, then we create only one object the thread class. And the same object we pass to multiple threads. Meaning, all threads share the same object of the thread class.

Let’s understand each with a thread example.

Threads Objects on Extending Thread class

In below java code example, we have created two threads t1 and t2 in main() method. class Task is the thread class.

Notice that for every thread we create, we must create different objects for them in the memory. e.g.

Task t1 = new Task();// thread 1
Task t2 = new Task();// thread 2

Java Code Example

//My task the tread class
class Task extends Thread {

	public void run() {
		System.out.println("\nTread is running the task\n");
	}
}

// TEST
public class Sample {

	public static void main(String[] args) {

		Task t1 = new Task();// thread 1
		t1.start();

		Task t2 = new Task();// thread 2
		t2.start();

	}
}

Threads Objects on Implementing Runnable Interface

Below example is same as above but using runnable interface. Notice that we have created only one object of the thread class Task. e.g. Task r = new Task();

and passing the same object r to 2 different threads t1 and t2. In other words, both t1 and t2 sharing the same object of the thread class “Task”.

Code Example:

//My task is the tread class
class Task implements Runnable {

	public void run() {
		System.out.println("\nTread is running the task\n");
	}
}

// TEST
public class Sample {

	public static void main(String[] args) {

		// Create one object of Task Thread class
		Task r = new Task();

		// create thread 1
		Thread t1 = new Thread(r);
		t1.start();

		Thread t2 = new Thread(r);
		t2.start();

	}
}

2) Memory Consumption of Threads

You have seen that we must create multiple objects of our thread class “Task” if we want to create multiple threads in above example when we extend the Thread class. But, in using Runnable interface example, only one object is created, and the same object of thread class “Task” is shared to multiple threads.

So, if the thread class is heavy (takes huge memory) then it is better to use Runnable interface.

Still, if you don’t understand how we are saving memory using Runnable Interfance, then read more on elaboration part given in bottom of the page in NOTES section.

3) Threads differences in case of Multiple Inheritance

We have already answer this before notes section that is if we extend thread class, we are losing a chance to extend another class. But if we use runnable interface, we save a chance to extend another class as multiple class cannot be extended. Java does not support multiple inheritance using classes but interfaces in java.

You can read the example how Multiple inheritance is achieved in java using interface.

4) Overriding threads methods facility

Sometimes we need to override the methods of Thread class. For example, start () method to write our own code inside that, then Extending thread class is option, whereas, it is not possible with Runnable interface.

You can read how and why we can override start () method of thread class in java program.

Conclusion

Difference between Extending Thread class vs implementing Runnable interface.

  • using Runnable interface give a chance to extend another class. Whereas, Extending Thread class doesn’t.
  • Saving memory using Runnable interface in case of multiple threads requirements. We don’t have to create multiple objects of our thread class.
  • Method overriding facility is available with Extending thread class and not with runnable interface.

NOTES:

Uses preference of Thread class and Runnable Interface:

Prefer Runnable Interface:

  • When don’t want to lose a chance for extending other classes
  • When don’t need to override methods of Thread class
  • When memory consumption is constraint during multiple threads creation.

Elaboration on saving memory on Extending Thread class and Implementing Runnable Interface:

It is difficult to understand for many people that how we are saving memory in above examples as in both cases the total number of objects created are 3.

Let me give you insight of it with simple example.

We cannot decide the memory benefits on the basis of total number of objects counts in the program.

This is true that total objects count is same in both cases (Task + Thread class). But, we need to consider the size of classes.

Let’s understand it Assuming an example:

Assume the size of both classes that “Thread” class and “Task” class.

Thread class size: 10 k

Task class size: 100 K as it may contain 50 of variables int, float and double etc.

Consider we are creating 20 threads in the program. Then on creation of 20 threads,

In case Extending Thread class – You create 20 objects of Task class resulting size = 20*100k =2000k

In case of implementing Runnable interface: You create 1 Task class object and 20 Thread class objects resulting size = 1*100k+ 20*10K = 100k+200k = 300K

So, for the same requirement, in case of extending “Thread” class, the total memory size is 2000k, whereas, in implementing Runnable interface, it is 300K

Hence, if we implement Runnable interface we save memory.

Related Posts