Given this code:
public class TwoThreads {
static Thread laurel, hardy;
public static void main(String[] args) {
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 {
laurel.wait();
} catch (Exception e) {
System.out.println("E");
}
System.out.println("F");
}
};
laurel.start();
hardy.start();
}
}
The output includes:
A C D E and F
I'm puzzled about why F is included, given that an IllegalMonitorStateException
is thrown when wait() is called outside of synchronized
code. Why is the print statement of F reached? I believe that the thread stack blows then, but then the program passes to its main stack, is this correct?