6633113: test/java/util/concurrent/SynchronousQueue/Fairness.java fails intermittently

Reviewed-by: dholmes
This commit is contained in:
Martin Buchholz 2008-03-10 23:23:48 -07:00
parent e4f30f8084
commit af3cf15e6e

View File

@ -23,36 +23,49 @@
/* /*
* @test * @test
* @bug 4992438 * @bug 4992438 6633113
* @compile -source 1.5 Fairness.java
* @run main Fairness
* @summary Checks that fairness setting is respected. * @summary Checks that fairness setting is respected.
*/ */
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public class Fairness { public class Fairness {
private static void testFairness(boolean fair, private static void testFairness(boolean fair,
final BlockingQueue<Integer> q) final BlockingQueue<Integer> q)
throws Exception throws Throwable
{ {
for (int i = 0; i < 3; i++) { final ReentrantLock lock = new ReentrantLock();
final Integer I = new Integer(i); final Condition ready = lock.newCondition();
new Thread() { public void run() { final int threadCount = 10;
try { q.put(I); } catch (Exception e) {} final Throwable[] badness = new Throwable[1];
}}.start(); lock.lock();
Thread.currentThread().sleep(100); for (int i = 0; i < threadCount; i++) {
final Integer I = i;
Thread t = new Thread() { public void run() {
try {
lock.lock();
ready.signal();
lock.unlock();
q.put(I);
} catch (Throwable t) { badness[0] = t; }}};
t.start();
ready.await();
// Probably unnecessary, but should be bullet-proof
while (t.getState() == Thread.State.RUNNABLE)
Thread.yield();
} }
for (int i = 0; i < 3; i++) { for (int i = 0; i < threadCount; i++) {
int j = q.take().intValue(); int j = q.take();
System.err.printf("%d%n",j);
// Non-fair queues are lifo in our implementation // Non-fair queues are lifo in our implementation
if (fair ? j != i : j != 2-i) if (fair ? j != i : j != threadCount - 1 - i)
throw new Exception("No fair!"); throw new Error(String.format("fair=%b i=%d j=%d%n",
fair, i, j));
} }
if (badness[0] != null) throw new Error(badness[0]);
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Throwable {
testFairness(false, new SynchronousQueue<Integer>()); testFairness(false, new SynchronousQueue<Integer>());
testFairness(false, new SynchronousQueue<Integer>(false)); testFairness(false, new SynchronousQueue<Integer>(false));
testFairness(true, new SynchronousQueue<Integer>(true)); testFairness(true, new SynchronousQueue<Integer>(true));