• 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

Threads

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks!!!
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
This is my favorite 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