Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

start() and join() in thread

 
ajith srikukan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic