--- jsr166/src/test/loops/CancelledProducerConsumerLoops.java 2005/12/10 20:08:51 1.3 +++ jsr166/src/test/loops/CancelledProducerConsumerLoops.java 2011/04/14 23:16:10 1.12 @@ -1,23 +1,13 @@ /* - * @test %I% %E% - * @bug 4486658 - * @compile -source 1.5 CancelledProducerConsumerLoops.java - * @run main/timeout=7000 CancelledProducerConsumerLoops - * @summary Checks for responsiveness of blocking queues to cancellation. - * Runs under the assumption that ITERS computations require more than - * TIMEOUT msecs to complete. - */ -/* * Written by Doug Lea with assistance from members of JCP JSR-166 - * Expert Group and released to the public domain. Use, modify, and - * redistribute this code in any way without acknowledgement. + * Expert Group and released to the public domain, as explained at + * http://creativecommons.org/publicdomain/zero/1.0/ */ - import java.util.concurrent.*; public class CancelledProducerConsumerLoops { static final int CAPACITY = 100; - static final long TIMEOUT = 100; + static final long TIMEOUT = 100; static final ExecutorService pool = Executors.newCachedThreadPool(); static boolean print = false; @@ -26,30 +16,30 @@ public class CancelledProducerConsumerLo int maxPairs = 8; int iters = 1000000; - if (args.length > 0) + if (args.length > 0) maxPairs = Integer.parseInt(args[0]); print = true; - + for (int i = 1; i <= maxPairs; i += (i+1) >>> 1) { System.out.println("Pairs:" + i); try { oneTest(i, iters); } - catch(BrokenBarrierException bb) { + catch (BrokenBarrierException bb) { // OK, ignore } Thread.sleep(100); } pool.shutdown(); - } + } static void oneRun(BlockingQueue q, int npairs, int iters) throws Exception { LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer); Future[] prods = new Future[npairs]; Future[] cons = new Future[npairs]; - + for (int i = 0; i < npairs; ++i) { prods[i] = pool.submit(new Producer(q, barrier, iters)); cons[i] = pool.submit(new Consumer(q, barrier, iters)); @@ -82,7 +72,7 @@ public class CancelledProducerConsumerLo long endTime = System.nanoTime(); long time = endTime - timer.startTime; if (print) { - double secs = (double)(time) / 1000000000.0; + double secs = (double) time / 1000000000.0; System.out.println("\t " + secs + "s run time"); } } @@ -97,6 +87,9 @@ public class CancelledProducerConsumerLo System.out.print("LinkedBlockingQueue "); oneRun(new LinkedBlockingQueue(CAPACITY), pairs, iters); + if (print) + System.out.print("LinkedTransferQueue "); + oneRun(new LinkedTransferQueue(), pairs, iters); if (print) System.out.print("SynchronousQueue "); @@ -113,13 +106,13 @@ public class CancelledProducerConsumerLo oneRun(new PriorityBlockingQueue(ITERS / 2 * pairs), pairs, iters / 4); */ } - - static abstract class Stage implements Callable { + + abstract static class Stage implements Callable { final BlockingQueue queue; final CyclicBarrier barrier; final int iters; - Stage (BlockingQueue q, CyclicBarrier b, int iters) { - queue = q; + Stage(BlockingQueue q, CyclicBarrier b, int iters) { + queue = q; barrier = b; this.iters = iters; } @@ -145,7 +138,7 @@ public class CancelledProducerConsumerLo } static class Consumer extends Stage { - Consumer(BlockingQueue q, CyclicBarrier b, int iters) { + Consumer(BlockingQueue q, CyclicBarrier b, int iters) { super(q, b, iters); }