• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Thread question from K&B book

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Siva.
I understand now clearly.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic