title: Java multithreading lock is not working correctly
body: <p>The whole program is :</p>
  <pre><code>import java.util.concurrent.*;
  import java.util.concurrent.locks.*;
  
  public class AccountWithConditionsUser {
      private static Account account = new Account();
      public static void main(String[] args) {
          System.out.println(&quot;Thread 1\t\tThread 2\t\tBalance&quot;);
          ExecutorService executor = Executors.newFixedThreadPool(2);
          executor.execute(new DepositTask());
          executor.execute(new WithdrawTask());
          executor.shutdown();
          while(!executor.isShutdown()) {
          }
      }
      public static class DepositTask implements Runnable{
          public void run() {
              try {
                  while(true) {
                      account.deposit((int)(Math.random()* 10)+ 1);
                      Thread.sleep(1000);
                  }
              }
              catch(InterruptedException ex) {
                  ex.printStackTrace();
              }
          }
      }
      
      public static class WithdrawTask implements Runnable{
          public void run() {
              while(true) {
                  account.withdraw((int)(Math.random() * 10) + 1);
              }
          }
      }
      
      private static class Account{
          private static Lock lock = new ReentrantLock(true);
          private static Condition newDeposit = lock.newCondition();
          
          private int balance = 0;
          public int getBalance() {
              return balance;
          }
          
          public void withdraw(int amount) {
              lock.lock();
              System.out.println(&quot;-&gt;Withdraw Obtained Lock&quot;);
              try {
                  while(balance &lt; amount) {
                      System.out.println(&quot;\t\t\tWait for deposit&quot;);
                      newDeposit.await();
                  }
                  balance-=amount;
                  System.out.println(&quot;\t\t\tWithdraw &quot;+ amount + &quot;\t\t&quot; + getBalance());
              }
              catch(Exception e) {
                  e.printStackTrace();
              }
              finally {
                  System.out.println(&quot;-&gt;Withdraw Released Lock&quot;);
                  lock.unlock();
                  
              }
              
          }
          
          public void deposit(int amount) {
              lock.lock();
              System.out.println(&quot;-&gt;Deposit Obtained Lock&quot;);
              try {
                  balance+=amount;
                  System.out.println(&quot;Deposit &quot;+ amount + &quot;\t\t\t\t\t&quot; + getBalance());
                  newDeposit.signalAll();
              }
              catch(Exception e) {
                  e.printStackTrace();
              }
              finally {
                  System.out.println(&quot;-&gt;Deposit Released Lock&quot;);
                  lock.unlock();
              }
          }
      }
  }
  </code></pre>
  <p>And the sample part of the output I got for this code is:</p>
  <p>......</p>
  <p>......</p>
  <p>Withdraw Released Lock</p>
  <p>Withdraw Obtained Lock</p>
  <p>Wait for deposit</p>
  <p>Deposit Obtained Lock</p>
  <p>Deposit 9                   13</p>
  <p>Deposit Released Lock</p>
  <p>Withdraw 9      4</p>
  <p>Withdraw Released Lock</p>
  <p>.....</p>
  <p>.....</p>
  <p>The question here is how Deposit thread obtained lock after window obtained lock. Isn't it supposed to be that once a lock is obtained it can't be obtained by any other thread! Or is it because of the signal methods? Can anyone please explain how that output came?
  Thanks in advance :)</p>