I have this code :
public static void main(String[] args) throws InterruptedException {
Object obj = new Object();
obj.wait();
obj.notify();
}
please I didn't understand why it throws java.lang.IllegalMonitorStateException??
I have this code :
public static void main(String[] args) throws InterruptedException {
Object obj = new Object();
obj.wait();
obj.notify();
}
please I didn't understand why it throws java.lang.IllegalMonitorStateException??
In order to wait on or notify on an object, you must be in a synchronized block locking on the object.
The following code will work (run but not do anything as there are no threads to synchronize):
public static void main(String[] args) throws InterruptedException {
Object obj = new Object();
synchronized(obj){
obj.wait();
obj.notify();
}
}