34

The Java Thread itself implements a Java Runnable! and according to most of the experts over Internet, implements Runnable is preferred over extends Thread! even though we cannot use utilize Runnable in the sense of thread with out the Thread class!
Then why do we prefer to implement Runnable over extending Thread since in both cases the actual thread is stated by calling a Thread implemented method (i.e. start() or run()) although in case of Thread we are not truly "extending" the functionality of Thread by merely overriding the run() method?

I apologies if I sound confusing..!

Community
  • 1
  • 1
Tariq
  • 2,489
  • 11
  • 36
  • 61

5 Answers5

81

The most common difference is:

When you extend Thread class, you can’t extend any other class which you require. (As you know, Java does not allow inheriting more than one class). When you implement Runnable, you can save a space for your class to extend any other class in future or now.

However, the significant difference is.

When you extends Thread class, each of your thread creates unique object and associate with it. When you implements Runnable, it shares the same object to multiple threads.

Check this: http://manikandanmv.wordpress.com/tag/extends-thread-vs-implements-runnable/

Aashray
  • 2,753
  • 16
  • 22
  • Means in case if, specific to the application, in future or now, we don't have a need to inherit the already inherited class (inherited from Thread) and the application don't seem to face a memory issue, then Runnable and Thread, both should have equal opportunities..? – Tariq Mar 18 '13 at 07:24
  • 2
    An OOP, the basic principle is, "Always program to an interface over an implementation" – footy Nov 18 '13 at 15:05
  • positive for the explanation at the link thanks for posting it here – Arjun Chaudhary Mar 20 '16 at 00:53
  • 1
    The example provided in the link clears the concept of diff between Thread extend & Runnable. Thanks! – Sangram Anand Dec 09 '16 at 00:40
8

If your class is extending the Thread class then it becomes a single thread which inherits the properties Thread class, so it'll be heavy. (When extending Thread class each of the threads creates unique object and associate with it, but when implementing Runnable, it shares the same object to multiple Threads).

If your class is Implementing the Runnable interface then you only override the run() .So this instance creates a separate Thread and every individual Thread runs separately but not as a single heavy Thread in your program. Another thing, Since Java does not support multiple inheritance, if you implement the Runnable you'll avoid problems of multiple extending, so if you implement Runnable interface you can extend any class that you are required other than Thread class.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • how about the memory consumption,... will be the execution of runnable and a new Thread() statement are actually same? (expensive if multiple call at once) ? – gumuruh Dec 07 '16 at 00:30
5

A class may only have one superclass, but may implement any number of interfaces. By extending Thread you give up the opportunity to subclass anything else.

minopret
  • 4,726
  • 21
  • 34
3

Java only allows single inheritance, which means that if you inherit from Thread you won't be able to inherit from any other class. Implementing the Runnable interface doesn't have this limitation, since your class is allowed to implement any number of interfaces.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
-5

you really sound confuse.Anyway one reason i know that as we can achieve the same functionality using Runnable, so we should go for it because if we will implement Runnable then we can extend other class which is not possible if we will extend Thread class

Android Killer
  • 18,174
  • 13
  • 67
  • 90