aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Join() method doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Join() method doubt" Watch "Join() method doubt" New topic
Author

Join() method doubt

Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
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

Joined: Jul 09, 2001
Posts: 1180
It's interesting. The output I see (with jdk 1.7) is -



Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
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

Joined: Jul 17, 2012
Posts: 39
Understood. Thanks Henry and Dan!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Join() method doubt