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

Infinite loop in timed Semaphore.tryAcquire

XMLWordPrintable

    • 5.0
    • b31
    • x86, sparc
    • solaris_9, windows_xp
    • Verified

        FULL PRODUCT VERSION :
        java version "1.5.0_02"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
        Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)


        A DESCRIPTION OF THE PROBLEM :
        Consecutive calls to Semaphore.tryAcquire(int permits, long timeout, TimeUnit unit) on a fair semaphore with no permits available causes infinite loop.


        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
            public static void main(String[] args) throws Exception {
                Semaphore sem = new Semaphore(0,true);
                System.out.println(sem.tryAcquire(100,TimeUnit.MILLISECONDS));
                System.out.println(sem.tryAcquire(100,TimeUnit.MILLISECONDS));
                System.out.println("OK!");
            }


        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        false
        false
        OK!
        ACTUAL -
        false


        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        There is no error message, as the second call to tryAcquire() never returns.

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.util.concurrent.*;
        public class Test {
            public static void main(String[] args) throws Exception {
                Semaphore sem = new Semaphore(0,true);
                System.out.println(sem.tryAcquire(100,TimeUnit.MILLISECONDS));
                System.out.println(sem.tryAcquire(100,TimeUnit.MILLISECONDS));
                System.out.println("OK!");
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Do not use fair semaphores (changing the above case to new Semaphore(0,false) works)
        ###@###.### 2005-03-16 22:22:49 GMT

              martin Martin Buchholz
              gmanwanisunw Girish Manwani (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: