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

Join() method doubt

 
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
Marshal
Pie
Posts: 20902
76
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic