View Single Post
  #1 (permalink)  
Old 03-31-2008, 07:03 PM
AegisCruiser AegisCruiser is offline
Member
 
Join Date: Mar 2008
Posts: 1
AegisCruiser is on a distinguished road
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();
}

}
Reply With Quote
Sponsored Links