title: java synchronization holding on to an object for specified interval of time
body: <p>In java can a thread hold on to an object for some specified interval of time ? For example, Suppose i have the following code: </p>
  
  <pre><code>package test;
  
  class Thread1 implements Runnable {
      private int balance;
      @Override
      public void run() {
          // TODO Auto-generated method stub
      for(int i=0;i&lt;50;i++)
      {
          increment();
  
          System.out.println("The bal by "+Thread.currentThread().getName()+" "+balance);
      }
      }
  
      public synchronized void increment()
      {
          int i=balance;
          balance =i+1;
      }
  
  
  }
  </code></pre>
  
  <p>Main class:</p>
  
  <pre><code>package test;
  
  public class MainC {
  public static void main(String args[])
  {
      Thread1 job=new Thread1();
      Thread a =new Thread(job);
      Thread b=new Thread(job);
      a.setName("Thread a");
      b.setName("Thread b");
      a.start();
      b.start();
  
  }
  }
  </code></pre>
  
  <p>Now, i want thread 1 to hold the lock on to the increment method till i==10 and don't want thread 2 to access the increment method! Is it possible!?? </p>