File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread vs Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread vs Runnable" Watch "Thread vs Runnable" New topic
Author

Thread vs Runnable

Gourab Guha
Greenhorn

Joined: Nov 06, 2006
Posts: 13
Hi All,

I know the basic difference between Thread and Runnable. If my class is extending some class but still we need to make it Thread, then we should use Runnable otherwise we can use any one.
But specifically I want to know is extending Thread gives any added advantage rather then implementing Runnable.
Please let me know the answer.

Regards,
Gourab
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This is often asked and we haven't come up with any good reason to extend Thread. In general, extending a concrete class is a dangerous thing, so implementing Runnable is A Good Thing.

I had a routine that had to keep references to Threads to join() and to the Runnables to get data and it was a bit of a pain to keep both. That was the only time in recent memory I was tempted to extend Thread. I still didn't.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
This question address the design aspects of programming. What you have specified is a rule of thumb about whether or not to extend Thread.

I would recommend that you understand the theoretical differences between classes and interfaces and how to implement that theory into practical situations. This is less related to the Thread class or the Runnable interface and more related to how to actually design a *good* system.

Goes to the core differences between classes and interfaces.

Yeah, that kind of stuff. This question is better answered (by yourself!) by stepping back and taking a wider perspective at the problem in hand.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
I am sure that you are not interested to change the behavior of a thread rather want to associate a worker with a thread.


Rahul Bhattacharjee
LinkedIn - Blog
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42916
    
  68
ExtendingThreadVsImplementingRunnable
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
The chief advantage of implementing an interface is that it enables the implementing class to extend any other class and still implement threading. But that depends on the requirements.

Another advantage could be that by implementing an interface, the class is forced to provide an implementation for the run() method, which is responsible for multithreading, while if you extend the class, you are not forced to override the run(), in case of which, a call to start() on the thread object will do nothing sort of multithreading.

Njoy!!
Sid
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Sidd Cool:
The chief advantage of implementing an interface is that it enables the implementing class to extend any other class and still implement threading. But that depends on the requirements.

Another advantage could be that by implementing an interface, the class is forced to provide an implementation for the run() method, which is responsible for multithreading, while if you extend the class, you are not forced to override the run(), in case of which, a call to start() on the thread object will do nothing sort of multithreading.

Njoy!!
Sid


All true, but these are implementation advantages. There are more important reasons which undoubtidly are listed in the FAQ cited.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread vs Runnable