This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes IllegalMonitorStateException from notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "IllegalMonitorStateException from notify()" Watch "IllegalMonitorStateException from notify()" New topic
Author

IllegalMonitorStateException from notify()

Joss Armstrong
Greenhorn

Joined: Jul 29, 2004
Posts: 17
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, OCMJEA
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40



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: 17
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
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IllegalMonitorStateException from notify()
 
Similar Threads
problem with wait()- notify()
wait and notify problem from K&B.
thread's example(about wait and notify) in kathy siera
After notify()
Multithreading: How can total be nearly 5,000 even though loop is up to 100?