Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-31-2008, 07:03 PM
Member
 
Join Date: Mar 2008
Posts: 1
Rep Power: 0
AegisCruiser is on a distinguished road
Default 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();
}

}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-23-2008, 07:16 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
Rep Power: 2
danielstoner is on a distinguished road
Default
Because probably the main thread calls the sendREP() method and sets ok = 1 and calls notify() before the thread t1 reaches the wait() call. Put a timeout on wait (wait(100)) and call wait in a loop, replace the if with a while.
__________________
Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use sleep() to wait for a while Java Tip java.lang 0 04-09-2008 07:32 PM
Package/Execution woes... Hiro ZA New To Java 2 01-09-2008 08:42 AM
Method execution time javaplus Advanced Java 3 11-26-2007 10:51 AM
Execution cut Eric Advanced Java 1 06-27-2007 04:52 PM


All times are GMT +2. The time now is 01:43 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org