• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Diffrence Between extending Thread & implementing Runnable interface?

 
Greenhorn
Posts: 18
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also see ExtendingThreadVsImplementingRunnable
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I personally like using Executors.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Executor.html
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic