If you want to notify or wait on an object, your thread must own the monitor of the object you act on.
public synchronized void callD(C cObj){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
cObj.callC1();
cObj.notify();
}
In this code section you synchronize on the instance of class D
, as synchronized methods always obtain the monitor of the object they "live" on. But to be able to use cObj.notify()
you have to obtain the monitor of the cObj
instance, e.g. by doing
synchronized(cObj) {
cObj.notify();
}