title: synchronized block locking object and wait/notify
body: <p>According to what I understood, when I use a synchronized block it acquires the lock on an object and releases it when the code block is done executing. In the following code </p>
  
  <pre><code>public class WaitAndNotify extends Thread{
  
      long sum;
  
      public static void main(String[] args) {
          WaitAndNotify wan = new WaitAndNotify();
          //wan.start();
          synchronized(wan){
              try {
                  wan.wait();
              } catch (InterruptedException ex) {
                  Logger.getLogger(WaitAndNotify.class.getName()).log(Level.SEVERE, null, ex);
              }
              System.out.println("Sum is : " + wan.sum);
          }
      }
  
      @Override
      public void run(){
          synchronized(this){
              for(int i=0; i&lt;1000000; i++){
                  sum = sum + i;
              }
              notify();
          }
  
      }  
  }
  </code></pre>
  
  <p>what happens if the synchronized block inside the run method acquires the lock first? Then the synchronized block inside the main method has to wait (not because of the wait(), because the other thread acquired the lock). After the run method is done executing, won't the main method enter its synchronized block and wait for a notify which it will never get? What did I misunderstand here? </p>