One question. I am studying for the
java certification exam and I found this question on Kathy Sierra Book.
I got this quote from the book:
wait(), notify(), and notifyAll() must be called from within a synchronized
context! A
thread can't invoke a wait or notify method on an object unless it owns
that object's lock.
And in the thread questions of the book at the end of the chapter question #12 has a wait() that is called outside synchronized context.
Do you know why this works and compiles?
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();
}
}
Self
Test 767