• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

start() and join() in thread

 
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
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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...
 
Ranch Hand
Posts: 194
  • 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...



http://nikojava.wordpress.com/2008/09/08/scjp-mock-exam-for-threads/
question 58

 
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?




 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic