In the following snippet
synchronized(myMonitorObject)
try{
myMonitorObject.wait();
} catch(InterruptedException e){...}
}
if the first line is omitted why the compiler does not object but instead IllegalMonitorStateException is thrown?
In the following snippet
synchronized(myMonitorObject)
try{
myMonitorObject.wait();
} catch(InterruptedException e){...}
}
if the first line is omitted why the compiler does not object but instead IllegalMonitorStateException is thrown?
Consider the following example:
public void m1(Object lock) throws InterruptedException {
lock.wait();
}
Should that be a compilation error?
Now consider this:
public void m2() throws InterruptedException {
Object lock = new Object();
synchronized (lock) {
m1(lock);
}
}
Should m1
still be a compilation error?
The problem with making m1
a compilation error (as you seems to be suggesting) is that what it is doing is not necessarily incorrect. In fact, it is impossible to for the compiler to reliably distinguish correct from incorrect without being conservative. The analogies are: