• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Extending Thread class

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread class implements Runnable interface. So when we wxtend Thread class Run() must be defined.
Following code sample is taken from mocktest in another section. In this code, run() is not implemented. Is not that a compile time error ? When I read the answer, it says code will run fine, but will not print anything.
 
Ranch Hand
Posts: 153
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread implements run(), but does nothing.
When extending classes, you don't have to implement their methods. You may overwrite them, but you don't have to. So a call to the run() method of a Bground object will call Thread's implementation of run(), which simply does nothing.
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jan Hoppmann wrote:Thread implements run(), but does nothing.


Actually it does something. If you create a Thread with a Runnable as argument to the constructor, it will call run() on that Runnable. If you don't provide a Runnable then it does nothing. Part of the source code for Thread, taken from src.zip:
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to tell what the method run does.

Take the code you wrote in the start method and put it in the run() method.

 
Greenhorn
Posts: 29
Android Opera Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think nirjari's question is whether extending the Thread class implies having to implement the run() method. This is not the case, your extended class inherits the run() method which is already in the Thread class. I think you confuse the 2 ways of defining a thread:

1) Extend the Thread class and override the run() method.
2) Implement the Runnable interface and implement the run() method.
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic