Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6855216

monitor acquisition unfairness and possible thread starvation

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6u11
    • hotspot
    • x86
    • linux

      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 ----------

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: