Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks!!!
 
Shin Kudo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When your class extends Thread, it can't extend any other classes. That's why the Runnable's way is prefered
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic