| Author |
IllegalMonitorStateException from notify()
|
Joss Armstrong
Greenhorn
Joined: Jul 29, 2004
Posts: 16
|
|
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
|
SCJP 6, OCPWCD JEE5, OCEWSD JEE6
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16684
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Joss Armstrong
Greenhorn
Joined: Jul 29, 2004
Posts: 16
|
|
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
Joined: Oct 19, 2009
Posts: 252
|
|
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]
|
"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Nidhi Sar
Ranch Hand
Joined: Oct 19, 2009
Posts: 252
|
|
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
Joined: Nov 04, 2009
Posts: 2066
|
|
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
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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
Joined: Nov 04, 2009
Posts: 2066
|
|
Ankit wrote ...
Is this a question or answer??
This is correct? Please confirm?
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
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...
|
 |
 |
|
|
subject: IllegalMonitorStateException from notify()
|
|
|