wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Extending Thread or implementing Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Extending Thread or implementing Runnable" Watch "Extending Thread or implementing Runnable" New topic
Author

Extending Thread or implementing Runnable

Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
which is a better approach.

Extending the Thread Class or Implementing Runnable interface.

Akshay.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
We implement Runnable when just need to implement run() of the Runnable interface. if we need to use some other methods of thread then we go for Thread class. no other distinction.

java doc says "Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class"
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

If I am to be anal, then I would say

1. if you are creating a thread to do some work then its a Runnable.

2. If you are creating a new kind of Thread, then extend Thread.
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
adeel ansari:

java doc says "Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class"

They don't think what happens in the run() method is fundamental to the behavior of a Thread?
Alan Walker
Greenhorn

Joined: Feb 15, 2004
Posts: 26
You could argue that what happens in the run method is not fundamental because nothing ever needs to happen in the run method (of Thread), if you pass a reference to another Runnable object when constructing the Thread.


Chihuahua - Ajax-powered online word puzzle, <a href="http://chi.lexigame.com" target="_blank" rel="nofollow">http://chi.lexigame.com</a> <br />Letterbox Word Game Online, <a href="http://letterbox.lexigame.com" target="_blank" rel="nofollow">http://letterbox.lexigame.com</a> <br />Lexi Word Game for Java-enabled phones, <a href="http://www.lexigame.com" target="_blank" rel="nofollow">http://www.lexigame.com</a>
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
yes it is. but here its about enhancing or modifying the fundamental behavior.
Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
Thnx for the inputs.

2. If you are creating a new kind of Thread, then extend Thread


can u elaborate a different kind of Thread.

Akshay.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

The obvious case is, if the task is going to be executed by a thread pool or a task scheduler, then implement the runnable interface. There is no reason to extend the thread class, if no new thread will be started directly.

As for the rest, it is up to debate. There are many cases that would work equally well with both techniques. IMO, while I think the debate is interesting, I think it is more of a developer's preference.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
Any more specific inputs ?

Neeraj.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Akshay Bhatia:
Thnx for the inputs.



can u elaborate a different kind of Thread.

Akshay.


The elaboration would be what Warren said. If you are making some kind of change to the behavior of the class Thread (which does not include the run method) then you should extend Thread.

In other words, always use runnable, unless you can't.
Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
Apart from what java docs say about "extends Thread" and "implements Runnable", one obvious thing has NOT been discussed here.

Say, suppose you extend the Thread class ... Now what happens is, if you are developing a big application, you are actually making this class OUT OF INHERITANCE NOW ... In other words, this class can inherit no other class as it already extends Thread. (Java allows extending only one class).

Whereas, if we implement Runnable, then you hava the opening to, extend some other class, leading to a better OO design. Since you can implement any number of interfaces, there is always a better OO design, if you opt to implement Runnable rather than extending a class.

Ofcourse, if you want the Thread to behave specially, you got to extend Thread, in which case, you can forgo this OO design advantage.


Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
Thanks for inputs.

It will be great, if somebody can share their project experiences(application background ,what methods were overriden etc) for extending Thread class.

Akshay.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

Say, suppose you extend the Thread class ... Now what happens is, if you are developing a big application, you are actually making this class OUT OF INHERITANCE NOW ... In other words, this class can inherit no other class as it already extends Thread. (Java allows extending only one class).


This statement is absolutely true, but it is not as big as an issue as it implies -- you are not "taking this class out of inheritance" to the point that it can't be returned.

The class is not being locked into a very complex inheritance tree. It is just a single inheritance. If you need to inherit from something else later, it should be easy to convert it to be a runnable object then. Furthermore, Thread objects are also Runnable objects, so the application can hold threads in a runnable variable, regardless of which technique is being used.

Not saying which is better. Just saying that it is not as clear cut.

Henry
Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
-------------------------------------
Henry Wrote:
If you need to inherit from something else later, it should be easy to convert it to be a runnable object then.
-------------------------------------

Thats true. What you said is fine for a small application. But what I was talking is for a big application. Say, I have released my package full of classes to some third-party people where I have used "extends Thread". Now the client of my package, wants to specialise on my class, but he cannot do it. So, the package has to be altered at my end again, leading to a separate release, waste of time and money and above all leading to a poor OO design.

I agree that it requires just change of the keywords from 'extends Thread' to 'implements Runnable'(something more too). But when you think in macro level, as above you can see the demerits.

Extends Thread is better suitable, if you want to specialise your Thread or if your class in carefully thought and decided as, to be "final".

_Mani
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Extending Thread or implementing Runnable
 
Similar Threads
best way to create a thread
Extending Thread over Implementing Runnable
Clarification wanted: Extending Threads vs. Implementing Runnable
Use of implementing Runnable Interface
Thread vs Runnable