title: How to avoid synchronize in multithreaded environment?
body: <p>I am facing one issue related to multithreading because of shared code. I want to avoid syncronization. I saw so many threads related to AtomicInteger &amp; Semaphore. But havn't got clear idea about what way and how exactly it is better option than synchronization.</p>
  
  <p>Here is my simple code which i want to make thread safe.
  Class to create thread.</p>
  
  <pre><code>public class ThreadCheck implements Runnable {
  TestStaticVar var = new TestStaticVar();
  @Override
  public void run() {
      // TODO Auto-generated method stub
      try {
          var.holdOn();
      } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  public static void main(String args[]) {
      ThreadCheck t = new ThreadCheck();
      Thread t1 = new Thread(t);
      Thread t2 = new Thread(t);
      t1.setName("A");
      t1.start();
      t2.setName("B");
      t2.start();
  }}
  </code></pre>
  
  <p>Class to be executed by multiple threads.</p>
  
  <pre><code>public class TestStaticVar {
  Semaphore sem = new Semaphore(1);
  public void holdOn() throws InterruptedException{
      sem.acquire();
  
      System.out.println("Inside Hold on....."+Thread.currentThread().getName()+"==&gt; ");//+i.get());
      try {
            for (long i=0; i&lt;Integer.MAX_VALUE; i++) {
  
          }
           System.out.println(var1.toString());
      } catch (Exception e) {
      // TODO Auto-generated catch block
          e.printStackTrace();
      }
      System.out.println("Finished Hold on===="+Thread.currentThread().getName());
                sem.release(1);
      System.out.println("Execution Completed by =====&gt; "+Thread.currentThread().getName());
  }}
  </code></pre>
  
  <p>Any help is highly appriciate.</p>
  
  <p>Thanks,
  Rajesh</p>