Results 1 to 2 of 2
  1. #1
    AegisCruiser is offline Member
    Join Date
    Mar 2008
    Rep Power

    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
    //Poit B: Why does execution not return to this point after notify()?
    }catch (InterruptedException e) {}
    System.out.print("bbb"); //Pont C


    public void sendREP() {
    synchronized (this) {
    ok = 1;

    } //end class Timer

    public static void main(String args[])
    Timer t1 = new Timer();



  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Rep Power


    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 @ []
    Language is froth on the surface of thought

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts