Apr
5
2015

extend Thread and implement Runnable difference

In this post we will see what exactly the difference between extend Thread and implement Runnable. Most of people ans is simple which is After extending a class we do not have scope to extend another class. This answer is accurate.But there are some more in depth answer.In this post we will see what is main difference other than the concept of inheritance.

Implent runnable extend thread

implement thread name : implement thread 2 count :0
implement thread name : implement thread 2 count :1
implement thread name : implement thread 2 count :2
implement thread name : implement thread 2 count :3
implement thread name : implement thread 2 count :4
extend thread name : Thread1 count 0
extend thread name : Thread1 count 1
extend thread name : Thread1 count 2
extend thread name : Thread1 count 3
extend thread name : Thread1 count 4
extend thread name : Thread2 count 0
extend thread name : Thread2 count 1
extend thread name : Thread2 count 2
extend thread name : Thread2 count 3
extend thread name : Thread2 count 4
implement thread name : implent thread1 count :5
implement thread name : implent thread1 count :6
implement thread name : implent thread1 count :7
implement thread name : implent thread1 count :8
implement thread name : implent thread1 count :9

From above output we can see that when we use extend thread then each Thread has its own object for modification.So there is no chance that more than one thread can work on same object.Each thread have a separate memory area. The point is that threads can not share data.Each thread have its own data for modification.Thread can not share resources of same object.

When we use implement Runnable then threads may have conman data  if more than one thread working on same object.Thread have shared resources of object.In above example data “i” of object implement1 is shared between two thread.

About the Author: devender kumar

java/j2EE developer

Comments are closed.