| Author |
Theard Class and Runnable interface
|
M Mehta
Ranch Hand
Joined: Aug 03, 2006
Posts: 92
|
|
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
Joined: Mar 24, 2004
Posts: 35
|
|
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.
|
Reasonable people adapt themselves to the world. Unreasonable people attempt to adapt the world to themselves. All progress, therefore, depends on unreasonable people.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24040
|
|
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.
|
[Jess in Action][AskingGoodQuestions]
|
 |
 |
|
|
subject: Theard Class and Runnable interface
|
|
|