• 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

no need to call wait() inside synchronized!!!

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After answering the self test question no.17 of chapter 9 (Threads) in K&B, i realised that there is no need to call wait() method inside synchronised context provided the thread holds the lock of the object.


Am I right?
[ March 05, 2007: Message edited by: raja kanak ]
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you are not right
Actually in the code below

if you print the stack trace of the exception, it will be an IllegalMonitorState that means that you are calling wait without putting it into a synch block.
That is an explanation of the code ...
However, i did not get the point saying that it does not need a synch block if the thread holds a lock of the object. Actually, synch takes a lock on the object .. so it does not seem logical to say that you dont have a synch block but you hold a lock !!!
Anyways, the code posted will produce an exception when the thread hardy tries to wait on laurel without acquiring a lock on laurel.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitesh hit the nail on the head. If you actually want the code to wait (as opposed to throwing an exception), you need to have a lock on the object in question.

Andy
 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


if you print the stack trace of the exception, it will be an IllegalMonitorState that means that you are calling wait without putting it into a synch block.



Nitesh, the code compiles & runs gives output A D E F (in my machine). It may produce output in different order but the code compiles & runs fine.

Kindly check page 757 of K&B self test (chapter 9 - Threads), it confirms my point.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah "E" is printed in the catch block. If you print the stacktrace it will be an IllegalMonitorStateException.
 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nitesh Kant:
Yeah "E" is printed in the catch block. If you print the stacktrace it will be an IllegalMonitorStateException.



Sorry Nitesh, I got the point now. Thanks you very much.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raja,

Why does "E" get printed?

Because an Exception occured.

Which exception? IllegalMonitorStateException.

So you can call wait() without being synchronized on the relevant object. But the call will result in an IllegalMonitorStateException being thrown at runtime (which on being caught produces output "E" in the sample code).
 
reply
    Bookmark Topic Watch Topic
  • New Topic