Forums Register Login

Threads

+Pie Number of slices to send: Send
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?


+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
thanks!!!
+Pie Number of slices to send: Send
When your class extends Thread, it can't extend any other classes. That's why the Runnable's way is prefered
+Pie Number of slices to send: Send
 

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
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 879 times.
Similar Threads
start(),run()
Thread --Extend vs implements
Instantiating a Thread page no 679
thread with no parameters doubt
Relationship between Thread class and Runnable interface?
Thread Question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 03:50:02.