• Post Reply Bookmark Topic Watch Topic
  • New Topic

Join() method doubt  RSS feed

 
Kedar Pethe
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given code-


I am unable to understand the output.
How come at second output line after Thread-1 0, control shifts to main?? If join() call is after the for loop,
why is it getting called immediately after Thread-1 0
Shouldn't For loop be completed??
Which is the current thread when we think of invoking join() inside run() method??
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's interesting. The output I see (with jdk 1.7) is -



Regards,
Dan
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This code....



causes a thread to join with itself -- meaning asking a thread to wait until it is done and is no longer alive, before exiting from the join method. Of course, this can never happen -- it will be alive while waiting, and it will be waiting while alive. This is, in effect, waiting forever.

Henry
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kedar Pethe wrote:

How come at second output line after Thread-1 0, control shifts to main?? If join() call is after the for loop,
why is it getting called immediately after Thread-1 0
Shouldn't For loop be completed??
Which is the current thread when we think of invoking join() inside run() method??


These two loops simply run simultaneously.
 
Kedar Pethe
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood. Thanks Henry and Dan!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!