File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes start() and join() in thread 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 "start() and join() in thread" Watch "start() and join() in thread" New topic
Author

start() and join() in thread

ajith srikukan
Greenhorn

Joined: Aug 19, 2008
Posts: 14



can anyone explain the reason behind the output ...

1. one.start();

//output "0"

2.one.start();
two.start();

//output "0"

3. one.start();
two.start();
one.join();
two.join();
//output varies between 1000 and 2000


SCJP 1.6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Hi Ajith, welcome to Javaranch.

Ajith at ranch we have a rule that if you post a code from a mock exam, then you've to QuoteYourSources. So please tell us from where did you take that question so that we can help you...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
In scenarios 1 and 2, the main method executed first, and in the 3rd it waits until both client threads are finished. If you run scenario 1 and 2 more, the output should change. Threads are unpredictable.
ajith srikukan
Greenhorn

Joined: Aug 19, 2008
Posts: 14
Ankit Garg wrote:Hi Ajith, welcome to Javaranch.

Ajith at ranch we have a rule that if you post a code from a mock exam, then you've to QuoteYourSources. So please tell us from where did you take that question so that we can help you...


ajith srikukan
Greenhorn

Joined: Aug 19, 2008
Posts: 14
Ryan Beckett wrote:In scenarios 1 and 2, the main method executed first, and in the 3rd it waits until both client threads are finished. If you run scenario 1 and 2 more, the output should change. Threads are unpredictable.




thanks Ryan, i got the idae .....

one more thing ... in the 3rd scenario the main thread will wait until other two threads finish and both methods are synchronized so each thread(one and two) loop will run 1000 times. this implies , the loop will run 2000 times but the output is between 1000 and 2000 why is that?




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: start() and join() in thread