Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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

Thread question from K&B book

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The question is which letters will eventually appear somewhere in the output?


The answer is A, C, D, E, F.

I don't understand why E is in the output?
In the explanation it says, hardy has not synchronized on laurel. So I tried synchronized (laurel) in the code, then it prints A, C, D, F.
But I couldn't understand the concept here?
Please help.

Thanks.
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know when you are calling wait method it should be under the synchronized context.

That is the thread should own the lock on the object on which wait method is called.

Due to above reason it throws IllegalMonitorStateException which will be caught by the Exception block and hence prints the Letter E.
 
a alph
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Siva.
I understand now clearly.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva has already explained it very well. The only thing to remember is that wait method should always be called from within the synchronized context.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic