• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extending Thread or implementing Runnable

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which is a better approach.

Extending the Thread Class or Implementing Runnable interface.

Akshay.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it is. but here its about enhancing or modifying the fundamental behavior.
 
Akshay Bhatia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any more specific inputs ?

Neeraj.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Akshay Bhatia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-------------------------------------
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
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic