This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

start() in java

 
Sanath Ramesh kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the start() automatically call the run()??What happens if the developer explicitly calls run() and also please explain the difference between above two scenarios??
 
Tim Moores
Bartender
Posts: 2687
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling run directly will execute that method in the current thread. Only by calling start will a new thread be created for the run method to execute in.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanath Ramesh kumar wrote:Why does the start() automatically call the run()??


Because the code for start() calls the native method start0(), which has code that says, "Start a new thread of execution, and in it, call this object's run() method."

In other words, it's because that's how the code is written, just like any other time one method calls another.
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To those beginners who don't know what this post was about: It was about the start() and run() methods of the Thread class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic