javasynchronization

Why isn't calling wait(), notify() or notifyAll() without a synchronized block not a compiler error?


If we call wait(), notify() or notifyAll() on an object without a synchronized block, we get an IllegalMonitorStateException at runtime.

Why doesn't the compiler flag me if I try to call these methods without a synchronized block?


Solution

  • Calling those methods only requires that the current thread be the owner of the object`s monitor. However, that could mean calling a method without synchronized from within the context of another synchronized block.

    For example:

    public void doWait(Object o) {
        o.wait(); // you would like the compiler to flag this
    }
    
    // but in this case it is valid
    synchronized(this)
    {
        doWait(this);
    }
    

    In general, there is now way to know at compile time whether any piece of code will be executed when the current thread does not hold a particular monitor, which is likely why the compiler does not even try to flag this.