• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

extend THREAD / implement RUNNABLE.......which to use?

 
Rajendra Deshpande
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul,
How does one decide which option to use to create a thread from the 2 given below..
1.Extending the THREAD class
2.Implmenting the 'RUNNABLE' interface.
Thanks and regards,
-Rajendra.
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually I base this on whether I need to inherit from other
classes. If so, I implement Runnable, if not I extend Thread.
 
Gurpreet Aulakh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In most cases you would implement the runnable interface. To extend from the Thread class means you cannot extend from any other classes, so if your design changes ... good luck
Also, by extending Thread when all you really want is for the code to run in a separate thread is excessive. You are inheriting all the members of the Thread class. Ask yourself, do you really need them?
In summary, implement Runnable except in those rare cases you need the extra functionality of the Thread class.
 
Kalidas Pavi
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is at least one instance when you must implement runnable instead of inheriting the thread class.
When you are witing an applet inheriting the applet class ie MyClass extends Applet, you can't say Myclass extends Applet extends Thread since JAVA do not support multiple inheritence.
You must implement the runnable interface in this case. ie MyClass extends Applet implements Runnable.
Kalidas
 
Rajendra Deshpande
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, Gurpreet, Kalidas,
Thanks for responding to this thread. Gurpreet,you said that extending THREAD class results in inheriting all the class under
it. Does this in any way reduce the performance by putting extra overhead during runtime or anything of that sort. Thanks yet again.
-Rajendra.
 
Gurpreet Aulakh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I really didn't say that correctly. If something
IS A Thread then it should subclass Thread and not just implement
Runnable unless there is some MI issue. E.G. a TimerThread class
IS A Thread so should subclass Thread unless some mult. inher.
restriction prevents it. This is just OO design practice. The
performance effects are negligible - you don't inherit any other
subclasses of Thread just because you inherit from Thread.
Originally posted by Rajendra Deshpande:
Jim, Gurpreet, Kalidas,
Thanks for responding to this thread. Gurpreet,you said that extending THREAD class results in inheriting all the class under
it. Does this in any way reduce the performance by putting extra overhead during runtime or anything of that sort. Thanks yet again.
-Rajendra.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic