title: wait and notify with implementing Runnable and extending Thread example
body: <p>I am implementing the logic to understand wait and notify works i found something which i am not able to analyze when i am extending the class with thread then wait and notify is working as expected but when i am implementing the runnable interface then output will be differ </p>
  
  <pre><code>public class AdderThread extends Thread {
  
  int total;
  
  @Override
  public void run() {
  
      synchronized (this) {
          for (int i = 0; i &lt; 1000; i++) {
              total += i;
          }
  
          notify();
      }
  
  }
  
  
  
  
  
  public class WaitNotify {
  
  public static void main(String[] args) {
  
      AdderThread addrTh = new AdderThread();
      addrTh.start();
      // -1243309312
  
      System.out.println("Total without Wait() " + addrTh.total);
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>When Implementing Runnable gives me correct answer means it is waitng for the thread to complete the task but when i am using extends thread it is giving me answer as 0 i.e. it is not waiting for thread to complete the task.</p>
  
  <pre><code>public class WaitNotify {
  
  public static void main(String[] args) {
  
      AdderThread addrTh = new AdderThread();
      addrTh.start();
      // -1243309312
  
      System.out.println("Total Before Wait " + addrTh.total);
      synchronized (addrTh) {
  
          try {
              System.out.println("Waiting for Sum...");
              addrTh.wait();
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          System.out.println("Total " + addrTh.total);
      }
  
  }
  </code></pre>
  
  <p>}</p>