title: PingPong Program Java Multithreading
body: <p>I am trying to learn basic concept of Multi Threading. </p>
  
  <p>Why my ping pong program prints only Ping0 &amp; Pong0, Why notify() does not start the Ping Thread which is in Wait state?</p>
  
  <p>public class PingPong implements Runnable {
        String word;</p>
  
  <pre><code>  public PingPong(String word) {
      this.word = word;
    }
  
    public void run() {
  
      synchronized (this) {
        for (int i = 0; i &lt; 10; i++) {
          System.out.println(word + i);
          try {
            wait(); 
            notifyAll();
          } catch (Exception e) {
            System.out.println(e.getMessage());
          }
        }
      }
    }
  
    public static void main(String[] args) {
  
      Runnable p1 = new PingPong("ping");
      Thread t1 = new Thread(p1);
      t1.start();
  
      Runnable p2 = new PingPong("pong");
      Thread t2 = new Thread(p2);
      t2.start();
  
    }
  
  }
  </code></pre>
  
  <p>Output</p>
  
  <pre><code>ping0
  pong0
  </code></pre>
  
  <p>I tried removing wait() and it's printing ping pong till the end of loop. but is this guaranteed that it will print in sequence?</p>
  
  <p>Why wait() followed by notify()  does not ask the ping1 thread to start execution?</p>