IllegalMonitorStateException

java.lang.IllegalMonitorStateException

Quick Answer

You called wait(), notify(), or notifyAll() on an object without holding its monitor. These methods must be called inside a synchronized block on the same object.

Why This Happens

The wait/notify mechanism requires the calling thread to own the object's monitor (lock). You must call wait() or notify() inside a synchronized block that synchronizes on the same object. Calling them outside synchronized context or on a different object causes this exception.

The Problem

Object lock = new Object();
lock.wait(); // IllegalMonitorStateException - not in synchronized block

The Fix

Object lock = new Object();
synchronized (lock) {
    lock.wait(); // Must be inside synchronized(lock)
}

Step-by-Step Fix

  1. 1

    Identify the unsynchronized call

    Find the wait(), notify(), or notifyAll() call that is not inside a synchronized block.

  2. 2

    Check the synchronized object

    If inside a synchronized block, verify that you are synchronizing on the same object you are calling wait/notify on.

  3. 3

    Add proper synchronization

    Wrap the wait/notify call in a synchronized block on the same object, or consider using java.util.concurrent utilities instead.

Got the actual stack trace?

Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.

Explain my error

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free