Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-31-2008, 07:03 PM
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();
}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-23-2008, 07:16 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 183
danielstoner is on a distinguished road
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 @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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 +3. The time now is 06:47 PM.


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