According to Oracle Javadoc:
IllegalMonitorStateException
is thrown when a thread has attempted to wait on an object’s monitor or to notify other threads waiting on an object’s monitor without owning the specified monitor.
In other words, this exception can be thrown only in case of bad design because Object.wait(...)
, Object.notify()
and
Object.notifyAll()
methods should never be called on an object whose monitor is not held.
public void doSomething(){ ... try { ... anObject.notify(); ... } catch(IllegalMonitorStateException e) { ... } }
public void doSomething(){ ... synchronized(anObject) { ... anObject.notify(); ... } }