Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IllegalMonitorStateException from notify()

 
Joss Armstrong
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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]

 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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;
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit wrote ...

Is this a question or answer??


This is correct? Please confirm?
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic