| 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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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?
|
 |
 |
|
|
subject: start() and join() in thread
|
|
|