title: Handle single object with two different threads with desired output
body: <p>If I want to update single object with two different threads how can I do ?</p>
  
  <p>I tried :</p>
  
  <pre><code>public class BankAccount{
  private static int balance;
  
  private static void balance(int amt)throws InterruptedException{
      balance = amt;
  
      Thread thread = new Thread(new Runnable(){
      @Override
      public void run(){
  
          try {
              balance(10);
              System.out.println("Initial Balance:\t"+balance);
          } catch (InterruptedException ex) {
              System.out.println("Exception"+ex.getMessage());
         }
  
      }
  
  });
          thread.start();
          TimeUnit.SECONDS.sleep(5);
  }
   private static void deposit(int deposit){
          balance+=deposit;
      }
  
  public static void main(String[] args)throws InterruptedException {
      Thread thread = new Thread(new Runnable(){
          @Override
          public void run() {
  
              deposit(10);
              System.out.println("Balance after deposit\t"+balance);
          }
  
      });
          thread.start();
          TimeUnit.SECONDS.sleep(5);     
  }
  
  }
  </code></pre>
  
  <p>I know using single thread it's possible to get desired output :<p>
  Initial Balance: 10<p>
  Balance after deposit: 20</p>
  
  <p>But if there is way to handle with two different threads.</p>