| Author |
when to use runnable?
|
william kane
Ranch Hand
Joined: Nov 21, 2000
Posts: 260
|
|
I know that extending Thread class creates a Thread and implementing the Runnable interface allows you to state the code in a class that will run parallel in the thread to which it is target. Instpite of being aware of this fact I am still not able to concieve a situation which mandates the need for me to implementing the Runnable interface in a class. Can anyone help?Explanation with the aid of an example will be very helpful thanx in advance william
|
Help me!Help you!!!
|
 |
David O'Meara
Rancher
Joined: Mar 06, 2001
Posts: 13459
|
|
Single inheritance in Java means you can only extend one class. Therefore if you always extend the Thread class, you cannot extend anything else. If you implement the Runnable interface instead, you are free to extend any class you want and implement any other interfaces as well. Personally, I never extend Thread, even when there isn't a class I'm extending. Dave
|
[ JavaRanch FAQ ][ Book Promotions ][ DbTamer ][ BumperStickers ][ JavaRanch Badges ]
|
 |
william kane
Ranch Hand
Joined: Nov 21, 2000
Posts: 260
|
|
thanx David, yes that restriction of single inhertance being a reason for implementing Runnable is anyway there. What I wanted was ,when i implementing runnable i place all the code that is intended to run in the calling thread parallel to the parent thread,where does this situation arise? can i get an example? P.S pl bear with my overly lenghty sentence.
|
 |
Nandkishore Dhilde
Greenhorn
Joined: May 09, 2002
Posts: 20
|
|
Rule of Thumb: If your class must subclass of some other class(except Thread) , you should use Runnable [ May 16, 2002: Message edited by: Nandkishore Dhilde ] [ May 16, 2002: Message edited by: Nandkishore Dhilde ]
|
SCJP1.4 certified, IBM SOA Solution Designer[2007]
|
 |
David O'Meara
Rancher
Joined: Mar 06, 2001
Posts: 13459
|
|
Originally posted by Nandkishore Dhilde: Rule of Thumb: If your class must subclass of some other class(except Thread) , you should use Runnable
...but my argument is that if it is a standard solution to all problems with no real side effects (small code overhead), when not always implement Runnable? Why bother with extending Thread at all?
|
 |
David O'Meara
Rancher
Joined: Mar 06, 2001
Posts: 13459
|
|
Originally posted by william kane: when i implementing runnable i place all the code that is intended to run in the calling thread parallel to the parent thread,where does this situation arise?
Sorry, I didn't understand. Can you repeat that for me? Dave
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18652
|
|
...but my argument is that if it is a standard solution to all problems with no real side effects (small code overhead), when not always implement Runnable? Why bother with extending Thread at all? Well, for a really simple thread that you want to implement quickly with an anonymous class, it's slightly more concise to extend Thread directly rather than implementing Runnable and then creating a Thread. It's a very minor benefit though. I can't think of any case where you really need to extend Thread rather than implement Runnable - but there are plenty of cases where either is equally suitable, and it's a matter of preference. If I were designing the language, I'd say don't let people override run() in Thread; force them to use Runnable. But that's not what they did, and at this point it's not really worth worrying about, IMO.
|
"I'm not back." - Bill Harding, Twister
|
 |
Junilu Lacar
Ranch Hand
Joined: Feb 26, 2001
Posts: 3008
|
|
The obvious reason to subclass Thread of course would be when the subclass really is a thread that runs concurrently with other threads. E.g. the garbage collection thread or the finalizer thread. You might also want to create a daemon or a worker thread that can continuously accept and execute Runnables. As mentioned already, in most cases you probably want to implement Runnable but the point is, a Thread is still an object and as such, you should think through whether it makes sense to make your new class a subclass of it or not. The tough part about learning Threads is that most of the examples in tutorials and books that you find out there start out with just inheriting from Thread. As Jim said, this approach is slightly more concise and makes for shorter code listings. The down side is that it doesn't always reflect actual recommended practice and that can be confusing for those just learning. Try reading intermediate to advanced level books (see Doug Lea, Allen Holub, Paul Hyde) for more "practical" examples. HTH, Junilu
|
 |
 |
|
|
subject: when to use runnable?
|
|
|