• 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

start() method in Thread

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai all
i have a doubt regarding the start() method of Thread class. start() implicitly calls the run() method of that class. o.k
but what is the problem if i override the start().
why in my follwing code after completing the start method in PrintNumThread
class the control fails to go to the run() method ???
plz clarify.
ritwik


class TestThread
{
public static void main(String arg[])
{
PrintNumThread pnt=new PrintNumThread();
pnt.start();
}
}
class PrintNumThread extends Thread
{
public void start()
{
System.out.println("PrintNumThread Started");

}
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println(i);
}

}
}
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why in my follwing code after completing the start method in PrintNumThread class the control fails to go to the run() method ???


It fails precisely because you've overridden the start method with the behavior you specify.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ritwik,
Well, the problem is this. Overridden method calls are always resolved at runtime based on the object which calls the method.

You have declared an object of the PrintNumThread class in the main() method as :


So, pnt is the object. Had you not overridden the start() method, the default action of pnt.start() would be to call the run() method of the PrintNumThread class.

However, since you have overridden the start() method in the PrintNumThread class, so at runtime, the object pnt would call the new overridden start() method and not the default start() method.

That's why the control does not reach the run() method since your overridden start() has only one statement:


Hope this helps !!
 
ritwik roy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Steve and sherry.
My doubt is cleared. But why then in java API the start() method has not
been declared final??
we know final method can't be overriden and in this type of case we must not override the start() method, so it should have been declared final.
what do u think???
reply
    Bookmark Topic Watch Topic
  • New Topic