dcsimg
When to use Runnable, not Thread class, any special case ?.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   manjul_dixit
Posted On:   Thursday, September 14, 2006 08:34 AM

Can you tell me a sceanario when we using Runnable Inteface is more ideal solution than extending Thread Class.

Re: When to use Runnable, not Thread class, any special case ?.

Posted By:   WarnerJan_Veldhuis  
Posted On:   Thursday, September 14, 2006 10:32 AM

From a OO design perspective, you extend a class if you want to extend or enhance it's functionality (There are ofcourse exceptions to this rule). You implement the Runnable interface when you have an object that you want to have a certain behaviour. So 9 out of 10 that you would use the Runnable interface.



If you want to run something in a seperate thread, my choice would be to implement the Runnable interface. It's a better OO design this way. Besides, extending Thread seriously limits my possiblities of a good class hierarchy...

Re: When to use Runnable, not Thread class, any special case ?.

Posted By:   Robert_Lybarger  
Posted On:   Thursday, September 14, 2006 08:46 AM

If the class you want the "run()" method in is already a subclass of someone else, you can't also extend Thread. Hence Runnable. That's really the biggest thing. On the other hand, you can usually put the "run()" in another class and work up an event notification scheme between them, so its partly up to your design preferences.
About | Sitemap | Contact