title: unfair synchronization among threads
body: <p>I have a code pattern like this:</p>
  <pre><code>import java.util.Timer;
  import java.util.TimerTask;
  import java.util.concurrent.atomic.AtomicInteger;
  
  public class TestRunner {
      public static final Object lockObject = new Object();
  
      public static final AtomicInteger counter1 = new AtomicInteger();
      public static final AtomicInteger counter2 = new AtomicInteger();
  
      public static class Runner1 implements Runnable {
          private volatile boolean running = true;
          @Override
          public void run() {
              while (running) {
                  synchronized (lockObject) {
                      counter1.getAndIncrement();
                      try {
                          Thread.sleep(10);
                      } catch (Exception e) {
  
                      }
                  }
                  // Thread.yield();
              }
          }
  
          public void stop() {
              running = false;
          }
      }
  
      public static class Runner2 extends TimerTask {
          @Override
          public void run() {
                  synchronized (lockObject) {
                      counter2.getAndIncrement();
                  }
          }
      }
  
      public static void main(String[] args) throws Exception {
          Runner1 runner1 = new Runner1();
          Runner2 runner2 = new Runner2();
  
          Thread thread1 = new Thread(runner1);
          Timer timer = new Timer();
          timer.scheduleAtFixedRate(runner2, 0, 10);
  
          thread1.start();
  
          Thread.sleep(10000);
          timer.cancel();
          runner1.stop();
  
          int runner1Percent = (counter1.get()*100 /(counter1.get()+counter2.get()));
          System.out.println(&quot;No of times Runner1 got called: &quot; + counter1.get());
          System.out.println(&quot;No of times Runner2 got called: &quot; + counter2.get());
          System.out.println(&quot;Percentage Runner1 acquire the lock: &quot; + runner1Percent);
          System.out.println(&quot;Percentage Runner2 acquire the lock: &quot; + (100-runner1Percent));
      }
  }
  </code></pre>
  <p>I run this code for about 10 second and here is the output:</p>
  <pre><code>No of times Runner1 got called: 877 
  No of times Runner2 got called: 10 
  Percentage Runner1 acquire the lock: 98 
  Percentage Runner2 acquire the lock: 2
  </code></pre>
  <p>As indicated here, Thread1, is able to acquire the lock 98% while the timer, is only
  able to acquire the lock 2%.</p>
  <p>If I uncomment out the Thread.yield() statement and rerun it, the output
  shows:</p>
  <pre><code>No of times Runner1 got called: 858
  No of times Runner2 got called: 1001
  Percentage Runner1 acquire the lock: 46
  Percentage Runner2 acquire the lock: 54
  </code></pre>
  <p>This shows that Thread1 and Timer share the lock more uniformly. Does this mean the jvm has some optimization/caching so that Thread1 is able to keep acquiring the lock? If I am not able to replace the synchronized block due to some other restrictions, is Thread.yield() a right approach to have the threads sharing the lock more uniformly?</p>
  <p>Any suggestions would be helpful.</p>