Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

start()

 
kumarth ravi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a some basic doubt.



My doubt is:
-------------
here start() is inherited from the threadclass.It will access only the methods and variables of it's class.Here how this start() method calls the subclass MyThread's run() method.
I think it calls only the superclass run method();


with regards.
ravi
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it will call the run method that is defined in class as far down in the class hierarchy of the current object as possible. So if your MyThread subclass overrides the run method, that one will be called. That's the whole purpose of polymorphism.

This behavior actually isn't specific to threads at all - moving to Java in General...
 
frens java
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The start() in java.lang.Thread calls a native method start0(), which sets up requisite datastructure for thread. Once native method is done with what it has to do, it calls the run() in java.lang.Thread.
java.lang.Thread.run() sees if the subclass of Thread overrides run() and executes the overriden method.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by frens java:

java.lang.Thread.run() sees if the subclass of Thread overrides run() and executes the overriden method.


Actually it's not Thread.run() which looks for it, but the JVM which looks for whether the method is overridden. Thread.run() actually does nothing at all (take a look at its source code that comes with the JDK).
 
kumarth ravi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your answers.

with regards
ravi
 
kumarth ravi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your answers.

with regards
ravi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic