• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

IllegalMonitorStateException from notify()

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please can someone explain to me why the following code gives an IllegalMonitorStateException when the notify method is called?

N.B. It works correctly when a public Object o is instantiated in the Example1 class and this is used to synchronize on.

view plaincopy to clipboardprint?



Thanks

Joss Armstrong
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Remember that synchronization is based on objects (and not references)..... hint.... is the object whose notify() method is called, the same as the object that is used to synchronize on?

Henry
 
Joss Armstrong
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand now that this error is caused by the updating of the object within the synchronized method.

Thank you very much for your help.

Joss Armstrong
 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I understand now that this error is caused by the updating of the object within the synchronized method.



Hold on a moment... why can't you update an object that you hold a lock to within synchronized method??

If we change the code to synchronize on example1 instead of example1.total, it works fine. But updating total is updating the state of example1 too. [After all total is an instance variable of Example1 class]

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nidhi Sar wrote:Hold on a moment... why can't you update an object that you hold a lock to within synchronized method??


I hope that you know the difference between object and reference. Keeping that in mind, you can update an object to which you hold the lock. Example

The reason Joss' code didn't work is because we called the notify method on a different object and synchronized on a different object. The reference was the same, but the object was different. The original code does a work similar to the following code
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:The reason Joss' code didn't work is because we called the notify method on a different object and synchronized on a different object. The reference was the same, but the object was different.



Hi Ankit,

Thanks so much! That's great explanation. Somehow, I thought the following line was just changing the state of example1.total Integer object. But ofcourse, it creates a new object (unboxing, adding i, boxing) and gives it the same reference. My mistake

example1.total += i;
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, All....

If we don't synchronized both blocks(which have wait() and notify() methods within them), We don't get any Compilation error. But the 'communication' between them doesn't work. OK

 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:If we don't synchronized both blocks(which have wait() and notify() methods within them), We don't get any Compilation error. But the 'communication' between them doesn't work. OK


Is this a question or answer??
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit wrote ...

Is this a question or answer??



This is correct? Please confirm?
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we don't call wait or notify method in synchronized context (synchronized on the object on which we are calling wait or notify), then we don't get any compilation error but an IllegalMonitorStateException at runtime...
 
Can't .... do .... plaid .... So I did this tiny ad instead:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic