Must wait() and notify() be written in a synchronized block?
posted 12 years ago
There must be some sort of synchronization. Both these method will throw an IllegalMonitorStateException if called from code that doesn't already hold the lock for whatever object is being used to invoke wait() or notify(). Which meanse that in order to have previously acquired this lock, you must be inside a synchronized block or synchronized method. Or, inside a method that was called from within another method that had already acquired the lock, using a synchronized method or block, etc. It's possible to create examples where the synchronization might be many levels removed from the method that calls wait() or notify(), possibly even in another class. Though that's probably a bad idea for readability...