• Post Reply Bookmark Topic Watch Topic
  • New Topic

Diffrence Between extending Thread & implementing Runnable interface?

 
Sathish Kumar Govindan
Greenhorn
Posts: 18
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello group,

I had found two answers for the difference between extending Thread and implementing Runnable interface........

1) If our class is already extending another class we can't extend Thread class because java doesn't support multiple inheritance, so we have left only one choice that is implementing Runnable interface....
2) when you extend a thread each of your thread has a unique object associated with it, whereas with Runnable, many threads share the same object instance..

I can't understand the 2nd point...

Thanks in advance,
G.Sathish kumar.
 
Henry Wong
author
Sheriff
Posts: 22516
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2) when you extend a thread each of your thread has a unique object associated with it, whereas with Runnable, many threads share the same object instance..

I can't understand the 2nd point...


Well, it would help if you explain why you can't understand the 2nd point. It seems straightforward. You can instantiate one runnable object, and then instantiate many thread objects (passing it the same runnable object). With runnable, many threads can share the same runnable instance.

Henry
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also see ExtendingThreadVsImplementingRunnable
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For point 2, it should say that many threads can share the same Runnable instance. They certainly don't have to. Sometimes a Runnable class also has instance variables, and in this case it would usually be a bad idea to share the same instance across multiple threads.
 
Anirudh Vyas
Ranch Hand
Posts: 93
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!