IllegalMonitorStateException on wait call
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
IllegalMonitorStateException on wait() Call
Understanding the IllegalMonitorStateException is crucial when dealing with multithreading in Java, particularly when working with thread synchronization using methods like wait(), notify(), and notifyAll(). This article delves into the underlying reasons for this exception, practical examples, and best practices to avoid it.
Overview
The IllegalMonitorStateException in Java is an unchecked exception that typically occurs during synchronization when a thread tries to perform an operation without holding the appropriate monitor lock. The exception specifically states that a thread is in an illegal state to call a certain monitor operation.
Common Scenario: The wait() Method
Explanation
The wait() method is used in thread communication. It makes the current thread wait until another thread calls notify() or notifyAll() on the same object. For a thread to call wait() on an object, it must hold the monitor lock of that object.
IllegalMonitorStateException Cause
The IllegalMonitorStateException is thrown if a thread attempts to call wait() on an object's monitor it does not own. Owning the monitor means the thread has synchronized on the object using a synchronized block or method.
Example
Below is an example that demonstrates the IllegalMonitorStateException in the context of a wait() call:
In the incorrectWait() method, calling wait() on the lock object will throw an IllegalMonitorStateException because the current thread does not have the monitor lock on lock. In contrast, correctWait() synchronizes on lock, ensuring the current thread has the monitor before calling wait().
Related Methods: notify() and notifyAll()
Just like wait(), the notify() and notifyAll() methods require the current thread to own the object's monitor. Failing to do so will result in an IllegalMonitorStateException.
Avoiding IllegalMonitorStateException
- Synchronize properly: Always ensure that the current thread holds the monitor lock of an object before calling
wait(),notify(), ornotifyAll()on it. - Use synchronized blocks or methods: These constructs inherently acquire and release the monitor lock, preventing accidental method calls without the monitor.
- Consistency: Ensure that wait and notify calls relate to the same lock object to avoid confusion.
Summary Table
| Operation | Condition | Exception Thrown |
wait() | No monitor held | IllegalMonitorStateException |
notify() | No monitor held | IllegalMonitorStateException |
notifyAll() | No monitor held | IllegalMonitorStateException |
| Synchronized Block Synchronized Method | Holds the monitor on start and releases at end | No exception if properly used |
Additional Details
- Thread State: When a thread calls
wait(), it transitions from the running to the waiting state. Once notified, it transitions back to runnable once the lock is reacquired. - Timeout Variants:
wait(long timeout)andwait(long timeout, int nanos)are overloaded variants where a thread will wait for the specified time if not notified sooner. - Interrupts and Exceptions: The
wait()method can throwInterruptedException, so it's essential to handle it, typically by reasserting the interrupt status.
Understanding the IllegalMonitorStateException and how to handle the wait() operation is vital for writing robust multithreaded applications in Java. By adhering to synchronization best practices, you can avoid common pitfalls and ensure efficient and safe inter-thread communication.
Related reading
- Illustrating usage of the volatile keyword in C
- implement a task based program in Java without the use of a clock
- Implementation of a work stealing queue in C/C?
- Implementing condition_variable timed_wait correctly
- Immutability of Strings in Java
- Immutable array in Java
- implements Runnable vs extends Thread in Java
- Implications of using MPI with TensorFlow

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.