title: Two threads executing synchronized block simultaneously
body: <p>Below is the code where a <code>Thread</code> enters a synchronized block, waits for 5 seconds and then exits. I have started two <code>Thread</code> instances simultaneously.</p>
  
  <p>The expectation was one of the threads will own the lock on the synchronized object, and the other will wait. After 5 seconds, when the lock owner exits, the waiting thread will execute.</p>
  
  <p>But, in actual, both the threads are executing the synchronized block simultaneously and also exiting at the same time.</p>
  
  <blockquote>
    <p>Expected Output:</p>
  </blockquote>
  
  <pre><code>Thread-X &lt;timeX&gt; received the lock.
  Thread-X &lt;timeX+5s&gt; exiting...
  Thread-Y &lt;timeY&gt; received the lock.
  Thread-Y &lt;timeY+5s&gt; exiting...
  </code></pre>
  
  <blockquote>
    <p>Actual Output:</p>
  </blockquote>
  
  <pre><code>Thread-X &lt;time&gt; received the lock.
  Thread-Y &lt;time&gt; received the lock.
  Thread-X &lt;time+5s&gt; exiting...
  Thread-Y &lt;time+5s&gt; exiting...
  </code></pre>
  
  <p>Am I missing something here?</p>
  
  <pre><code>import java.text.SimpleDateFormat;
  import java.util.Date;
  
  public class Test2 {
  public static void main(String[] args) {
      MyRunnable m = new MyRunnable();
      Thread t = new Thread(m);
      Thread t1 = new Thread(m);
      t.start();
      t1.start();
      }
  }
  
  class MyRunnable implements Runnable {
      @Override
      public void run() {
          synchronized (this) {
              try {
                  SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                  Date date = new Date(System.currentTimeMillis());
                  System.out.println(Thread.currentThread().getName() + " " + formatter.format(date) + " received the lock.");
                  wait(5000);
                  date = new Date(System.currentTimeMillis());
                  System.out.println(Thread.currentThread().getName() + " " + formatter.format(date) + " exiting...");
              } catch(InterruptedException ie) {}
          }
      }
  }
  </code></pre>