• 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

Thread

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why does this class compile since there is no run method defined
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rename the start method() to run()

hth
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Threads is class that implements the Runnable interface....and since Thread isnt an abstract class run method must be implemented in it....so you can call run on Thread class but it does nothing
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

no no i got it but its not what you said
actually we are extending thread class so its default run method is automatically inherited which does nothing ....
found this just now on a website
btw thanks
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.Thread class implements Runnable interface and Runnable interface is having abstract run() method which has to be implement in every class which extends to Thread or implement Runnable.
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh forget my last post its not true , thread class it self implements run() method you don't need to implement run() in your class which extends to thread class.

but in case of runnable if you implement runnable interface you have to implement run() method.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Minhaj kaimkhani wrote:...thread class it self implements run() method....

but in case of runnable if you implement runnable interface you have to implement run() method.



Yes. Thread does implement Runnable and it properly overrides the run() method, fulfilling the requirement to override run(). So when you extend Thread you inherit the perfectly good run() method in the Thread class. Note that while it fulfills the technical requirement to fulfill the run() method, it does not fulfill the practical need to create a usable run() method in your subclass. It compiles, but it's not useful.

On the other hand, if you create a class that implements Runnable, you need to override the run() method. Just in this case, you were technically inheriting Thread's run() method.

Ben
 
reply
    Bookmark Topic Watch Topic
  • New Topic