Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Internal Implementation of run()

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
I was unable to figure out the answer for the below question.Help provided will be appreciated.

We have the below method in Thread class





Assuming we have Thread t in place. we have to call run() method as t.start().
How will the JVM know that the t.start() method has to Invoke the run() method in this case.?Please clarify..

~Deepak Lal

 
Mos Jeff
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you have to create a class that either extends Thread or implements Runnable interface. Then overwrite the run method. Once you have done this you can refer to the Java API for Thread start(). The start method creates a new thread and executes this.run().
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default implementation of Thread.start() look for the given Runnable object on the Constructor if any. For more info check the Thread documentation,

Regards,
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look one here
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have seen peoples saying that start will call the run method and yes that is correct but partially

main things is that.

Start will create a new thread and that thread after initializing itself call run method.


Internal implementation of run method,
Runnable:-
Runnable is a interface so it doesnot contain implementation of run method.So any class which implements this interface must have to provide a implementation of run method.

Thread:-

It is class which simply implements a run method but doesnot do anything with it..
see below code



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic