Results 1 to 4 of 4
  1. #1
    davis123877 is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default Wait/Notify mechanism

    I wrote a simple program to test my understanding of wait/notify and it's not working as expected. I want it to print "ping", sleep for 5 seconds, then notify the other thread to print "pong". I am not sure what I am doing wrong as it just prints "ping" then hangs forever. Here is my code:

    class Ping extends Thread {
    static final Object lock = new Object();
    public void run() {
    synchronized(Ping.lock) {
    System.out.println("ping");
    try {
    Thread.sleep(5000);
    } catch (InterruptedException ie) {
    ie.printStackTrace();
    }
    lock.notify();
    }
    }
    }
    class Pong extends Thread {
    public void run() {
    synchronized(Ping.lock) {
    try {
    Ping.lock.wait();
    System.out.println("pong");
    Thread.sleep(5000);
    } catch(InterruptedException ie) {
    ie.printStackTrace();
    }
    }
    }
    }
    class PingPong {
    public static void main(String[] args) {
    Ping t1 = new Ping();
    Pong t2 = new Pong();
    t1.start();
    t2.start();
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Wait/Notify mechanism

    Please place your code between [code][/code] tags to format it. To do what you want to do you need to have one thread that waits forever. The other thread then sleeps for 5 seconds and then notifies the waiting thread.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Wait/Notify mechanism

    First observation: If this is supposed to go back and forth, you will need wait() and notify() in both Ping and Pong.
    Second observation: Your Threads run only once. Make an endless loop in run() (or specify a condition, like do it three times).

    Third observation (nitpick): only extend Thread if you add functionality to the class. You are not, you just need public void run(). It would be better to implement Runnable:
    Java Code:
    public class Ping implements Runnable {
       public void run() {
          ...
       }
    }
    
    public class PingPong {
       public static void main(String[] args) {
          Thread ping = new Thread( new Ping());
          Thread pong = new Thread( new Pong());
       }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Wait/Notify mechanism

    @Surfbabe: I do believe you are running ahead of things, the mission for now is to print ping and pong only once. I'm sure phase two is to make that repeat.

    @davis: As an exercise, try commenting out that Ping.lock.wait() line in the Pong class. See what your application does.


    EDIT2: as a matter of fact, add another System.out.println() which might help you to understand better:

    Java Code:
    class Pong extends Thread {
      public void run() {
    
        synchronized(Ping.lock) {
          System.out.println("Hi, I'm the Pong class entering the synchronized block. Take note of when exactly I am printed out.");
          try {
            // Ping.lock.wait();
            System.out.println("pong");
            Thread.sleep(5000);
          } catch(InterruptedException ie) {
            ie.printStackTrace();
          }
        }
      }
    }
    Last edited by gimbal2; 03-18-2015 at 10:07 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Wait() and Notify()
    By SiX in forum New To Java
    Replies: 15
    Last Post: 07-28-2011, 04:29 PM
  2. wait() and notify()
    By jomypgeorge in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 08:58 AM
  3. Need help with wait and notify
    By mityay in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-06-2011, 04:24 PM
  4. Need help with wait() and notify()
    By Mkaveli in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-30-2010, 11:58 AM
  5. wait() and notify() trouble with UI
    By Atriamax in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-09-2009, 02:51 AM

Posting Permissions

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