• 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

Theard Class and Runnable interface

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a doubt regarding Thread class and Runnable interface. We can extend from a Thread class or we can implement the Runnable interface for a class to follow multithreading. It is recommended that we use Runnable interface as my class will be able to extend from any other class in that case. But my doubt is that why do we have an option to extend from Thread class. Is there any scenario where extending from Thread is more preferable.

Thanks,
Mehta
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Extract from Java Doc over Runnable - Runnable provides the means for a class to be active while not subclassing Thread. A class that implements
Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run()method and no other Thread methods.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some people have called it simply a mistake that Thread has a public, nonfinal run() method of its own. There's no good reason why both mechanisms are allowed. It just confuses people.

Remember (and I've said this a lot lately) that the people who wrote the core Java API classes had no prior experience in writing in Java, and this shows in many of their design choices. Implementation mistakes have been fixed over the years, but the API design problems remain.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic