When and why is IllegalMonitorStateException thrown?

Alex Chaffee

According to the JavaDoc, IllegalMonitorStateException is thrown "to indicate that 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."

As explained in How do wait and notify really work? , in order to call foo.wait() or foo.notify(), the calling thread must own a lock on object foo. This exception is thrown if you call it without a preceding synchronized (foo) {.