aspose file tools*
The moose likes Threads and Synchronization and the fly likes extend THREAD / implement RUNNABLE.......which to use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "extend THREAD / implement RUNNABLE.......which to use?" Watch "extend THREAD / implement RUNNABLE.......which to use?" New topic
Author

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

Rajendra Deshpande
Ranch Hand

Joined: Nov 24, 2000
Posts: 40
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

Joined: Jan 05, 2001
Posts: 532
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

Joined: Jan 07, 2001
Posts: 12
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

Joined: Nov 20, 2000
Posts: 42
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

Joined: Nov 24, 2000
Posts: 40
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

Joined: Jan 07, 2001
Posts: 12
Yes.
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 532
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: extend THREAD / implement RUNNABLE.......which to use?
 
Similar Threads
threads
Thread question
Thread/ Runnable
Multithreading
about thread and Runnable interface