Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IllegalMonitorStateException

 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm not misstaken, wait() releases all locks that the current thread has.
/Mike
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I bet it is because you have synchronized a static method instead of an instance method.
Bill
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
" 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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it does not even compile with a plain wait()
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic