aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes IllegalMonitorStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "IllegalMonitorStateException" Watch "IllegalMonitorStateException" New topic
Author

IllegalMonitorStateException

Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
I must be missing the big picture on locks and synchronized code. I thought that when a thread began execution on a synchronized method, it obtained a lock to the method's object. The references I've seen have pointed out that the wait() and notify() methods must be placed within a block of synchronized code or within a syncrhonized method to avoid the IllegalMonitorStateException.
The wait() method in the code below generates an IMSE despite the fact that it is within a synchronized method, and the message suggests that the thread executing the synchronized method does not have a lock. I'm sure I don't know how to give a thread a lock if not to have it execute a synchronized method.
Can anyone clear up my confusion on this issue?

[This message has been edited by Scott Appleton (edited May 23, 2001).]
[This message has been edited by Scott Appleton (edited May 23, 2001).]
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
If I'm not misstaken, wait() releases all locks that the current thread has.
/Mike
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
That's the functionality I'm looking for and expecting. What I don't understand is why the wait() call is generating an IMSE from within the synchronized method.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
I bet it is because you have synchronized a static method instead of an instance method.
Bill
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Bill, in response to your suggestion I altered the code as follows (and removed the static keyword from the moveCount method signature):

However, the IMSE was still generated by the wait() call in the synchronized method of the Counter instance co.
An additional question would be why would the wait() call be valid in non-static synchronized methods but not in static synchronized methods?
[This message has been edited by Scott Appleton (edited May 23, 2001).]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
" synchronized static public void moveCount(MyThread th, int d) throws InterruptedException, SecurityException {
....
th.wait(d); // this line causes the IMSE
} // moveCount
Ah - I see you are calling th.wait() but you don't have
a lock on th, you have a lock on the object containing the moveCount method. Therefore - boom
Use just plain wait() - that calls wait in the object you have the lock on.
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
it does not even compile with a plain wait()


sona<br />SCJP
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Thanks, Bill, that was the problem. The IMSE disappeared with a simple wait(d) call.
I'm finding that the synchronized method doesn't appear to be synchronized, however. Both threads are calling the moveCount method at the same time (simultaneous sleeping states, and the static variable is identical for both). The behavior of the program is identical whether the moveCount method is synchronized or not.
Niels Campbell
Greenhorn

Joined: May 08, 2002
Posts: 3
If you had wanted to wait on the monitor of the th object. Then the wait would have to be in a synchronised block.
synchronized (th)
{
th.wait(d); // this line causes the IMSE
}
Cheers,
Niels
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IllegalMonitorStateException