About Question com.enthuware.ets.scjp.v6.2.705 :
Posted: Thu Mar 22, 2012 7:25 am
This assertion in explanation is not correct.
"So, wait()/notiy()/notifyAll() can only be called from synchronized methods because then it will be sure that the thread really has the lock for the object (resource)"
"Can only be called" is not true. This is incorrect. Of course they can be called!
The explanations about where the methods "wait", "notify" and "notifyAll" can be called, are confusing in some books (like Katty Sierra and Bates) and in some questions in this program.
When someone types "can only" or "must" speak in terms of obligation. The correct term is "should" because it speaks in terms of recommendation, there is no such thing as an "obligation" to indicate where these methods should be called like a compulsory.
In the book of Sierra and Bates usually use the verb "must" instead of should. You're always free to call this methods in a place of code out of a "synchronized block". Of course.
Is not necessary and is not an obligation to call this methods inside a synchronized context. The resultant IllegalMonitorStateException is other thing. It's is secondary, ¿the code compile? Yes, then call this methods out of synchronized blocks is possible, and correct for compile purposes.
"So, wait()/notiy()/notifyAll() can only be called from synchronized methods because then it will be sure that the thread really has the lock for the object (resource)"
"Can only be called" is not true. This is incorrect. Of course they can be called!
The explanations about where the methods "wait", "notify" and "notifyAll" can be called, are confusing in some books (like Katty Sierra and Bates) and in some questions in this program.
When someone types "can only" or "must" speak in terms of obligation. The correct term is "should" because it speaks in terms of recommendation, there is no such thing as an "obligation" to indicate where these methods should be called like a compulsory.
In the book of Sierra and Bates usually use the verb "must" instead of should. You're always free to call this methods in a place of code out of a "synchronized block". Of course.
Is not necessary and is not an obligation to call this methods inside a synchronized context. The resultant IllegalMonitorStateException is other thing. It's is secondary, ¿the code compile? Yes, then call this methods out of synchronized blocks is possible, and correct for compile purposes.