JDK-6855216 : monitor acquisition unfairness and possible thread starvation
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 6u11
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2009-06-26
  • Updated: 2011-02-16
  • Resolved: 2010-08-27
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_11"
Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
Java HotSpot(TM) 64-Bit Server VM (build 11.0-b16, mixed mode)

FULL OS VERSION :
Linux pixie 2.6.25-gentoo-r6 #4 SMP Fri Nov 14 15:47:41 CET 2008 x86_64 Dual Core AMD Opteron(tm) Processor 170 AuthenticAMD GNU/Linux

A DESCRIPTION OF THE PROBLEM :
As of JDK 1.6, lock are dispensed so that the thread current holding a lock is favored compared to other competing threads. This will cause other threads to starve.

  See also http://ceki.blogspot.com/2009/06/biased-locking-in-java-se-60.html

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Problem occurs anytime a resource protected by a synchronized block is accessed by multiple threads.


EXPECTED VERSUS ACTUAL BEHAVIOR :
Actual reaults
java.runtime.version = 1.6.0_11-b03
java.vendor          = Sun Microsystems Inc.
java.version         = 1.6.0_11
os.name              = Linux
os.version           = 2.6.25-gentoo-r6
runnable[0]: counter=1002
runnable[1]: counter=0
runnable[2]: counter=0
runnable[3]: counter=0
runnable[4]: counter=0

One would expect a more even distribution of the counter value.
REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class LockingInJava implements Runnable {

 static int THREAD_COUNT = 5;
 static Object LOCK = new Object();
 static Runnable[] RUNNABLE_ARRAY = new Runnable[THREAD_COUNT];
 static Thread[] THREAD_ARRAY = new Thread[THREAD_COUNT];

 private int counter = 0;

 public static void main(String args[]) throws InterruptedException {
   printEnvironmentInfo();
   execute();
   printResults();
 }

 public static void printEnvironmentInfo() {
   System.out.println("java.runtime.version = "
       + System.getProperty("java.runtime.version"));
   System.out.println("java.vendor          = "
       + System.getProperty("java.vendor"));
   System.out.println("java.version         = "
       + System.getProperty("java.version"));
   System.out.println("os.name              = "
       + System.getProperty("os.name"));
   System.out.println("os.version           = "
       + System.getProperty("os.version"));
 }

 public static void execute() throws InterruptedException {
    for (int i = 0; i < THREAD_COUNT; i++) {
      RUNNABLE_ARRAY[i] = new LockingInJava();
      THREAD_ARRAY[i] = new Thread(RUNNABLE_ARRAY[i]);
    }
    for (Thread t : THREAD_ARRAY) {
      t.start();
    }
    // let the threads run for a while
    Thread.sleep(10000);
    
    for (int i = THREAD_COUNT - 1; i <= 0; i--) {
      THREAD_ARRAY[i].interrupt();
    }
    Thread.sleep(100); // wait a moment for termination, to lazy for join ;)
  }

  public static void printResults() {
    for (int i = 0; i < RUNNABLE_ARRAY.length; i++) {
      System.out.println("runnable[" + i + "]: " + RUNNABLE_ARRAY[i]);
    }
  }

  public void run() {
    for (;;) {
      synchronized (LOCK) {
        counter++;
        try {
          Thread.sleep(10);
        } catch (InterruptedException ex) {
          break;
        }
      }
    }
  }

  public String toString() {
    return "counter=" + counter;
  }
}
---------- END SOURCE ----------

Comments
PUBLIC COMMENTS (1) Dialog I recently had relating to this bug is captured in the following blog comments: http://ceki.blogspot.com/2009/06/biased-locking-injava-se-60.html. (2) this has nothing to do with biased locking. I think we established that in the tests that I asked but run. I've editted the synopsis accordingly. (3) Arguably, this is either NAB or an RFE. It's clearly a duplicate of 4985566. Having said that it's possible we might be able to provide some relief, although ultimately the issue arises from a misunderstanding about the fairness properties of locks. (native mutexes on almost all systems exhibit precisely the same behavior as hotspot, btw).
26-06-2009