title: Java nested synchronized
body: <p>I have a problem to understand how works the nested synchronized.
  i give here 2 examples to make make a comparison:</p>
  
  <pre><code>//example 1
  public class Account {
      int balance = 1000;
      Object lock = new Object();
  
      public int getBalance() throws InterruptedException {
          synchronized(lock) {
              // step 1- Thread 1 enter here and sleep
              Thread.sleep(20);
              return balance;
          }
      }
      public void setBalance(int amount) {
          // step 2 - Thread 2 have to wait until thread 1 release the lock.
          synchronized(lock) {
              this.balance = amount;
          }
      }
  }
  </code></pre>
  
  <p>the example above is clear and logic.<br>
  now look the example 2:<br></p>
  
  <pre><code>public class Account {
      int balance = 1000;
      Object lock = new Object();
  
      public int getBalance() {
          synchronized(lock) {
              // step 1 - Thread 1 enter here and lock the object.
              synchronized(lock) { //same lock 
                  //step 2 - Thread 1 can enter here also if the lock is already locked why????
  
                  //...do something...
                  return balance;
              }
          }
      }
  }
  </code></pre>
  
  <p><br>I don't understand in the example 2, why the same thread can enter on the same lock 2 times if the outer lock is already locked...</p>