• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread execution  RSS feed

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Output:
Sorry, I must be leaving
hello


I have one doubt in the above code: Please tell my flow is correct or not:

1. Object for HelloThread() has been created
2. Setting the Daemon option true.
3. starting the thread. So the run() method willl invoke automatically.
4. Now it should print "hello"
5. Then return back to main method to print "Sorry, I must be leaving"

But I am getting different output. Why?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The order of steps 4 and 5 is unspecified. It's entirely possible that sometimes when you'll run the program they'll be reversed. But it's actually most likely that 5 happens first, because starting a new thread takes time. The run() method will only be invoked once Java has got the new Thread all set to go, and by that time, the original thread is very likely to have proceeded to the next statement.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!