From af3cf15e6ea376954f009e0f10351a40b4b42880 Mon Sep 17 00:00:00 2001 From: Martin Buchholz Date: Mon, 10 Mar 2008 23:23:48 -0700 Subject: [PATCH] 6633113: test/java/util/concurrent/SynchronousQueue/Fairness.java fails intermittently Reviewed-by: dholmes --- .../concurrent/SynchronousQueue/Fairness.java | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/jdk/test/java/util/concurrent/SynchronousQueue/Fairness.java b/jdk/test/java/util/concurrent/SynchronousQueue/Fairness.java index 2a8a35a83fe..5adb71d99f7 100644 --- a/jdk/test/java/util/concurrent/SynchronousQueue/Fairness.java +++ b/jdk/test/java/util/concurrent/SynchronousQueue/Fairness.java @@ -23,36 +23,49 @@ /* * @test - * @bug 4992438 - * @compile -source 1.5 Fairness.java - * @run main Fairness + * @bug 4992438 6633113 * @summary Checks that fairness setting is respected. */ import java.util.concurrent.*; +import java.util.concurrent.locks.*; public class Fairness { private static void testFairness(boolean fair, final BlockingQueue q) - throws Exception + throws Throwable { - for (int i = 0; i < 3; i++) { - final Integer I = new Integer(i); - new Thread() { public void run() { - try { q.put(I); } catch (Exception e) {} - }}.start(); - Thread.currentThread().sleep(100); + final ReentrantLock lock = new ReentrantLock(); + final Condition ready = lock.newCondition(); + final int threadCount = 10; + final Throwable[] badness = new Throwable[1]; + lock.lock(); + 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++) { - int j = q.take().intValue(); - System.err.printf("%d%n",j); + for (int i = 0; i < threadCount; i++) { + int j = q.take(); // Non-fair queues are lifo in our implementation - if (fair ? j != i : j != 2-i) - throw new Exception("No fair!"); + if (fair ? j != i : j != threadCount - 1 - i) + 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()); testFairness(false, new SynchronousQueue(false)); testFairness(true, new SynchronousQueue(true));