• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another Thread Question

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ranchers,



What i think is the output should be (Deat,Mr Gaunt,I Have Tested it and still have a doubt),
Numbers 1 to 5 will be printed in serial order with "N Done" anywhere in the sequence where N cannot be determined.

Please Confirm Whether i am thinking Right or Wrong.

Thanks,
With Regds,
Anand
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think you are right, but will your answer be the same if we remove synchronized?
 
anand phulwani
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it wont be, for that i am sure,
Thanks for helping out.

Thanks,
With Regds,
Anand
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about this one carefully: What object's lock is being used for the synchronized method run()? And then count how many objects there are.
 
anand phulwani
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Mr. Barry Gaunt,

What i think each will have lock of a new object which is created,that means every thread has a lock for itself,that means each can run the method run() even simultaneously and it might be that the numbers which are printed can be missed or even repeated.

Please Confirm Me by Posting,

Thanks,
With Regds,
Anand
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's correct. Unless the synchronization is on one unique object's lock, the behavior in unpredictable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic