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