Java - Call to start method on thread : how does it route to Runnable interface's run () ?
Posted
by Bhaskar
on Stack Overflow
See other posts from Stack Overflow
or by Bhaskar
Published on 2010-04-07T22:42:33Z
Indexed on
2010/04/07
22:43 UTC
Read the original article
Hit count: 254
Ok , I know the two standard ways to create a new thread and run it in Java :
1 Implement Runnable in a class , define run method ,and pass an instance of the class to a new Thread. When the start method on the thread instance is called , the run method of the class instance will be invoked.
2 Let the class derive from Thread, so it can to override the method run() and then when a new instance's start method is called , the call is routed to overridden method.
In both methods , basically a new Thread object is created and its start method invoked. However , while in the second method , the mechanism of the call being routed to the user defined run() method is very clear ,( its a simple runtime polymorphism in play ), I dont understand how the call to start method on the Thread object gets routed to run() method of the class implementing Runnable interface. Does the Thread class have an private field of Type Runnable which it checks first , and if it is set then invokes the run method if it set to an object ? that would be a strange mechanism IMO.
How does the call to start() on a thread get routed to the run method of the Runnable interface implemented by the class whose object is passed as a parameter when contructing the thread ?
© Stack Overflow or respective owner