6 Replies Latest reply: Sep 28, 2011 1:36 PM by jtahlborn RSS

    java.util.concurrent.Semaphore tryAcquire() bug

      The java bug database at bugs.sun.com is down, so I will post this here for now...

      In a nutshell, on Windows XP, java 1.6.0_27, the behavior of tryAcquire() with timeout is broken; the actual duration of the calls can vary from less than the timeout specified to up to 8x, but on average is about 2.5x. I couldn’t replicate the issue on Solaris and I haven’t tried any other combinations of JVM version / OS.

      Here is a modified version of the code from the comment in bug 6241823, instrumented with timing, that shows the problem:
      public class SemaphoreBug
      public static void main(String[] args) throws Exception {
      java.util.concurrent.Semaphore s = new java.util.concurrent.Semaphore(0, true);
      for (int i = 0; i < 100; i++) {
      long start = System.currentTimeMillis();
      s.tryAcquire(100L, java.util.concurrent.TimeUnit.MILLISECONDS);
      long duration = System.currentTimeMillis() - start;
      System.out.println("semaphore duration: " + duration);