• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Theard Class and Runnable interface

 
M Mehta
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • 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
 
Amit Ghai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic