File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes IllegalMonitorStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "IllegalMonitorStateException" Watch "IllegalMonitorStateException" New topic
Author

IllegalMonitorStateException

Mike Broadbear
Ranch Hand

Joined: Jan 14, 2002
Posts: 39
I am getting an IllegalMonitorStateException in this code when I run it on a multiprocessor. Can anyone tell me why?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Can you check or provide the stack trace? I don't think there's anything in the code you quoted that can throw an IllegalMonitorStateException.
- Peter
PS. What's wrong with "return (gvtFlag > 0);"?
Mike Broadbear
Ranch Hand

Joined: Jan 14, 2002
Posts: 39

line 214 is:

I can't use the simplified verion in this case because of the relaxed memory model. The synchronized block flushes reads and writes to the variable.
...Mike B.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
Hmmm... this really doesn't make sense to me. I'd go so far as to suggest that perhaps your class files aren't freshly compiled - try deleting them (at least, delete LocalOptProcessor.class) and recompile. Run again and see if you get the same message - I'm hoping it will be a different location. If that doesn't work, try posting more of the code from LocalOptProcessor.java, so we can understand the context better. Good luck...


"I'm not back." - Bill Harding, Twister
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 530
That exception is usually tied with a call to wait() somewhere. Are you calling wait() anywhere else in your code?
Mike Broadbear
Ranch Hand

Joined: Jan 14, 2002
Posts: 39
I am definitely calling wait() in the Processor class, which is the LocalOptProcessor superclass. I am not calling gvtState.wait() anywhere. But if that is the origin of the error, why is the stack trace pointing to the method I have listed here? And also, why is the stack trace incomplete?
Can anyone suggest any other sources that may be able to help me with this problem? Thanks.
...Mike B.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
Other methods that can throw this exception are notify() and notifyAll(). It still doesn't make sense though that the error comes from "parent.checkGVTFlag()", unless the the JVM was unable to provide a complete stack trace. Did you delete class files and recompile as I suggested? Try compiling with the -g option as well - that may help provide more info in the stack traces.
[ February 18, 2002: Message edited by: Jim Yingst ]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Regarding the weirdness of the stack trace, I was wondering if compiler optimisations might be the culprit here. Although I understand Java compilers don't tend to be very aggressive, a compiler may inline and even reorder code. That might well affect the trace.
Seconding Jim's suggestion of compiling with -g; in addition, make sure optimization is turned off. Use a different compiler if need be.
- Peter
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I have refactored most of the monitors into helper classes that use fully synchronized methods. I seem to be getting far fewer of these kinds of errors now. I can only theorize that there is some problem with synchronizing a block of code on an object instance on the particular system that I am using. (SGI Origin 2000) If anyone knows anything more, I would love to hear it. Thanks.
...Mike
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
One hard-to-spot pitfall is synchronizing on a reference to a non-mutable object, and then later in trying to "change the state of the object" you really change the object, so subsequent monitor-related calls on the new reference produce an error. Here's a thread that discusses this issue:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=27&t=000553


Rob
SCJP 1.4
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for the lead Rob. I am not sure if that is occuring in my code or not. I will definitely look deeper into it.
I have narrowed down at least one instance of the IllegalMonitorStateException, however. It seems to be happening when thread A calls interrupt on thread B, while thread B holds a lock on an Object instance inside a synchronized block. Again, it is not happening in the classes that utilize fully synchronized methods. I am not sure how interupt may interfere with the locking mechanism.
...Mike B.
 
 
subject: IllegalMonitorStateException
 
Threads others viewed
ExamLab Exception
Threads
q on interfaces from overalltesting.com
Thread Problem
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture