Why does notifyAll() raise IllegalMonitorStateException when synchronized on Integer?

You have noted correctly that notifyAll must be called from a synchronized block.

However, in your case, because of auto-boxing, the object you synchronized on is not the same instance that you invoked notifyAll on. In fact, the new, incremented foo instance is still confined to the stack, and no other threads could possibly be blocked on a wait call.

You could implement your own, mutable counter on which synchronization is performed. Depending on your application, you might also find that AtomicInteger meets your needs.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)