Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

when to use runnable?  RSS feed

 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!