¿Por qué este programa de prueba da como resultado un java.lang.IllegalMonitorStateException
?¿Por qué notifyAll() genera IllegalMonitorStateException cuando se sincroniza en Integer?
public class test {
static Integer foo = new Integer(1);
public static void main(String[] args) {
synchronized(foo) {
foo++;
foo.notifyAll();
}
System.err.println("Success");
}
}
Resultado:
Exception in thread "main" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at test.main(test.java:6)
que no se dieron cuenta de que incrementar el número entero asignaría un nuevo objeto en lugar de cambiar el valor del objeto existente. – jjvainio
Esta es una de las muchas opciones de Autoboxing/unboxing. –
Intentar esperar/notificar usando un Enum también puede llevar a esta condición: 'synchronized (myEnum) {myEnum = MyEnum.NEW_VALUE; myEnum.notify(); } ' – dmitrii