title: How to create fair lock to solve starvation in java?
body: <p>I want to create fair lock so that each thread was given a lock one after the other regardless of priorities.</p>
  
  <pre><code>import java.util.concurrent.locks.ReentrantLock;
  
  public class StarvationRunnable implements Runnable {
  
      private ReentrantLock lock = new ReentrantLock(true);
  
      public void doLongTask() {
          lock.lock();
          // to imitate long running task in critical section
          for (int i = 0; i &lt; 1000000000; i++)
              ;
          System.out.println(Thread.currentThread().getName() + " is running with priority "
                  + Thread.currentThread().getPriority() + " !");
          lock.unlock();
      }
  
      @Override
      public void run() {
          for (;;) {
              doLongTask();
          }
      }
  
      public static void main(String[] args) {
          StarvationRunnable runnable = new StarvationRunnable();
  
          for (int i = 0; i &lt; 4; i++) {
              Thread thread = new Thread(runnable);
              thread.setPriority(i == 3 ? Thread.MIN_PRIORITY : Thread.MAX_PRIORITY);
              thread.start();
          }
      }
  }
  </code></pre>
  
  <p>So I have 4 threads. 3 with max priority and 1 with min priority. I create fair lock by</p>
  
  <pre><code>new ReentrantLock(true);
  </code></pre>
  
  <p>When I run this demo it doesn't give thread with min priority to execute as many times as to threads with max priority. But I think that fair lock is just for that.</p>
  
  <p>How to use fair lock correctly?</p>