Win a copy of The Java Performance Companion this week in the Performance 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: 2839
46
  • 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
Posts: 21120
32
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