In the statement below, the wait()
method is executed even though no notify is called but the statement below wait()
are executing only after laurel
thread completes its execution.
I tried using other objects for locking in synchronization of hardy block this time wait method is still waiting forever, can someone explain me why statement after wait()
was executed?
package delagation;
public class Solution extends Thread {
static Thread laurel, hardy;
public static void main(String[] args) throws InterruptedException {
laurel = new Thread() {
public void run() {
System.out.println("A");
try {
hardy.sleep(1000);
} catch (Exception e) {
System.out.println("B");
}
System.out.println("C");
}
};
hardy = new Thread() {
public void run() {
System.out.println("D");
try {
synchronized(laurel) {
laurel.wait();
//wait method is called here,
//There is not notify in this class,
//but the statement below are executing
System.out.println(Thread.currentThread().getName());
}
} catch (Exception e) {
System.out.println("E");
}
System.out.println("F");
}
};
laurel.setName("laurel");
hardy.setName("hardy");
laurel.start();
hardy.start();
}
}