|
What is the execution path of wait() and notify() ?
(This is a repost from 'New to Java' because I think this probably is the right place for my question. Sorry for this repost.)
I have a very simple class as shown in the code below to test thread synchronization. The program prints only "bbb". Why does it not print "aaabbb"? After notify() is called, why does the program not return to point B? If I put a print clause before wait(), it does not print, either. Why is the print sequence not Point A, Point B, Point C?
This is totally different from what I just learned for thread synchronization. I'm new to java and please help. Thank you!
public class Test {
public static class Timer extends Thread {
public int ok = 0;
public void run() {
synchronized (this) {
if (ok == 0) {
try {
//Point A
wait();
//Poit B: Why does execution not return to this point after notify()?
System.out.print("aaa");
}catch (InterruptedException e) {}
}
System.out.print("bbb"); //Pont C
}
}
public void sendREP() {
synchronized (this) {
ok = 1;
notify();
}
}
} //end class Timer
public static void main(String args[])
{
Timer t1 = new Timer();
t1.start();
t1.sendREP();
}
}
|