title: Why does the awakened thread always preempt the lock first?
body: <p>I have a problem with synchronized as follows.</p>
  <pre><code>public class Test {
      private final static Object LOCK = new Object();
  
      public void startThreadA() {
          new Thread(() -&gt; {
              synchronized (LOCK) {
                  System.out.println(Thread.currentThread().getName() + &quot;: get lock&quot;);
                  //start thread-B
                  startThreadB();
                  System.out.println(Thread.currentThread().getName() + &quot;: start wait&quot;);
                  try {
                      //thead-A wait
                      LOCK.wait();
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  System.out.println(Thread.currentThread().getName() + &quot;: get lock after wait&quot;);
                  System.out.println(Thread.currentThread().getName() + &quot;: release lock&quot;);
              }
          }, &quot;thread-A&quot;).start();
      }
  
      private void startThreadB() {
          new Thread(() -&gt; {
              synchronized (LOCK) {
                  System.out.println(Thread.currentThread().getName() + &quot;: get lock&quot;);
                  //start thread-C
                  startThreadC();
                  try {
                      Thread.sleep(200);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  System.out.println(Thread.currentThread().getName() + &quot;: start notify&quot;);
                  //notify thread-A
                  LOCK.notify();
                  System.out.println(Thread.currentThread().getName() + &quot;: release lock&quot;);
              }
          }, &quot;thread-B&quot;).start();
      }
  
      private void startThreadC() {
          new Thread(() -&gt; {
              System.out.println(Thread.currentThread().getName() + &quot;: thread c start&quot;);
              synchronized (LOCK) {
                  System.out.println(Thread.currentThread().getName() + &quot;: get lock&quot;);
                  System.out.println(Thread.currentThread().getName() + &quot;: release lock&quot;);
              }
          }, &quot;thread-C&quot;).start();
      }
  
      public static void main(String[] args) {
          new Test().startThreadA();
      }
  }
  </code></pre>
  <p>When thread-B releases the lock, both thread A (Waken up by thread-B) and thread C (thread-B <strong>startThreadC()</strong>) are <strong>blocked</strong> trying to acquire the lock</p>
  <p>I ran the following code many times in JDK 8 on Windows and Linux, The running results:</p>
  <pre><code>thread-A: get lock
  thread-A: start wait
  thread-B: get lock
  thread-C: thread c start
  thread-B: start notify
  thread-B: release lock
  thread-A: get lock after wait
  thread-A: release lock
  thread-C: get lock
  thread-C: release lock
  </code></pre>
  <p>I think it is unfair that thread-A and thread-C both have a chance to preempt the lock. Why does thread-An always acquire the lock when thread-B releases the lock, What's the problem?</p>