Junior — Middle
Is it possible to use the notify method on an Object without owning the synchronized lock?
sobes.tech AI
Answer from AI
No, the notify() method of the Object object cannot be called without owning the monitor (lock) of that object, that is, without using synchronized. If notify() is called outside a synchronized block, an IllegalMonitorStateException will be thrown.
Example of correct usage:
synchronized(obj) {
obj.notify();
}
This is because notify() notifies one of the threads waiting on the object's monitor, and proper operation requires owning that monitor.