wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
hey,

i have doubt in difference between implementing runnable and extending Thread class?

what i understood by reading is while we implement runnable , we use Thread class reference and pass the reference of the run() method thread, and we need to call the run() explicitlly,even when we use it inside a consructor...

but in using extending Thread class we need not to pass such reference and we only need create the reference for run() thread class if we use the constructor for that class... we even need not to call the run() methjod explicitlly....

this what i have understood!!! am i correct ? please make me clear about this?


avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

vijay umar wrote:

we need to call the run() explicitlly,even when we use it inside a consructor...


i think in any of the two cases,we never have to call the run method explicitly rather in both cases we have to type call for start.

as per i know in case when we extend the thread class the object created is just ready to act like a new thread & we can write

MyClass c1=new MyClass(); c1.start(); if Myclass extends class Thread
in case of implementing Runnable we will have to write
Thread c1=new Thread(new MyClass());


avi


SCJP 5.0 SCWCD 5.0
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
thanks!!!
Shin Kudo
Greenhorn

Joined: Apr 17, 2009
Posts: 25
When your class extends Thread, it can't extend any other classes. That's why the Runnable's way is prefered


SCJP 6 - SCWCD in progress...
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Shin Kudo wrote:When your class extends Thread, it can't extend any other classes. That's why the Runnable's way is preferred


Yes very true. But there is one more important factor to this. When do we sub-class a class...when we want a more specific behaviour of the super-class...so sub-class the thread class only when you want a more specific behaviour of the thread class or else implement the Runnable interface and override the run(). So be careful about how you choose to implement the thread behaviour (OO Concepts are the heart of Java ) .

Hope this helps


Regards, Sud.
SCJP 5 ScjpFAQ JLS
 
 
subject: Threads