title: Thread execution sequence on synchronized block
body: <p>In below code I'm trying to figure out threads handle execution on synchronized blocks:</p>
  
  <pre><code>public class ThreadExp {
      public static void main(String[] args) throws InterruptedException {
          MyRunnable r = new MyRunnable();
          Thread t1 = new Thread(r, "Thread1");
          Thread t2 = new Thread(r, "Thread2");
          t1.start();
          t2.start();
      }
  
  }
  
  class MyRunnable implements Runnable {
  
      @Override
      public void run() {
          callFn();
      }
  
      private void callFn() {
          System.out.println(Thread.currentThread().getName() + ": entered callSync()");
          synchronized (Thread.currentThread()) {
              System.out.println(Thread.currentThread().getName() + ": inside sync");
              try {
                  Thread.currentThread().sleep(5000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              System.out.println(Thread.currentThread().getName() + " leaving sync");
          }
          System.out.println(Thread.currentThread().getName() + ": leaving callSync()");
      }
  }
  </code></pre>
  
  <p><strong>Actual Output:</strong></p>
  
  <pre><code>    Thread1: entered callFn()
      Thread2: entered callFn()
      Thread1: inside sync block
      Thread2: inside sync block
  // 5000 millisec sleep
      Thread2 leaving sync block
      Thread1 leaving sync block
      Thread2: leaving callFn()
      Thread1: leaving callFn()
  </code></pre>
  
  <p><strong>Whereas I expected something like:</strong></p>
  
  <pre><code>Thread1: entered callFn()
  Thread2: entered callFn()
  Thread1: inside sync block
  // 5000 millisec sleep
  Thread1 leaving sync block
  Thread1: leaving callFn()
  Thread2: inside sync block
  Thread2 leaving sync block
  Thread2: leaving callFn()
  </code></pre>
  
  <p>Overall I thought Thread1 will acquire the lock then go to sleep. And only after Thread1 is finished, Thread2 can enter the synchronized block.</p>