notifyAll() throws IllegalMonitorStateException

You’re calling wait and notify without synchronizing on the thing you’re waiting on or notifying. As documented in Object.notifyAll:

Throws:
IllegalMonitorStateException – if the current thread is not the owner of this object’s monitor.

So this:

synchronized(nameSetLock){
  notifyAll();
}

should be:

synchronized(nameSetLock){
  nameSetLock.notifyAll();
}

… and ditto for wait. Note that your current code wouldn’t even compile as you’re using syncronized rather than synchronized, which suggests that you didn’t post your actual code. It’s possible that in typing out the code you’ve actually changed the problem – in which case you should edit your question to be more representative.

Leave a Comment