--- jsr166/src/test/loops/TimeoutProducerConsumerLoops.java 2009/10/29 23:09:08 1.4 +++ jsr166/src/test/loops/TimeoutProducerConsumerLoops.java 2009/11/16 04:16:43 1.6 @@ -5,7 +5,7 @@ */ import java.util.concurrent.*; -//import jsr166y.*; + public class TimeoutProducerConsumerLoops { static final int NCPUS = Runtime.getRuntime().availableProcessors(); @@ -21,6 +21,10 @@ public class TimeoutProducerConsumerLoop intPool[i] = Integer.valueOf(i); } + // max lag between a producer and consumer to avoid + // this becoming a GC test rather than queue test. + // Used only per-pair to lessen impact on queue sync + static final int LAG_MASK = (1 << 12) - 1; static boolean print = false; static int producerSum; @@ -109,16 +113,18 @@ public class TimeoutProducerConsumerLoop final int iters; final BlockingQueue queue; final CyclicBarrier barrier; - Stage (BlockingQueue q, CyclicBarrier b, int iters) { + final Phaser lagPhaser; + Stage (BlockingQueue q, CyclicBarrier b, Phaser s, int iters) { queue = q; barrier = b; + lagPhaser = s; this.iters = iters; } } static class Producer extends Stage { - Producer(BlockingQueue q, CyclicBarrier b, int iters) { - super(q, b, iters); + Producer(BlockingQueue q, CyclicBarrier b, Phaser s, int iters) { + super(q, b, s, iters); } public void run() { @@ -136,6 +142,8 @@ public class TimeoutProducerConsumerLoop ++i; if (timeout > 1) timeout--; + if ((i & LAG_MASK) == LAG_MASK) + lagPhaser.arriveAndAwaitAdvance(); } else timeout++; @@ -151,8 +159,8 @@ public class TimeoutProducerConsumerLoop } static class Consumer extends Stage { - Consumer(BlockingQueue q, CyclicBarrier b, int iters) { - super(q, b, iters); + Consumer(BlockingQueue q, CyclicBarrier b, Phaser s, int iters) { + super(q, b, s, iters); } public void run() { @@ -171,6 +179,8 @@ public class TimeoutProducerConsumerLoop ++i; if (timeout > 1) --timeout; + if ((i & LAG_MASK) == LAG_MASK) + lagPhaser.arriveAndAwaitAdvance(); } else ++timeout; @@ -190,8 +200,9 @@ public class TimeoutProducerConsumerLoop LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer); for (int i = 0; i < npairs; ++i) { - pool.execute(new Producer(q, barrier, iters)); - pool.execute(new Consumer(q, barrier, iters)); + Phaser s = new Phaser(2); + pool.execute(new Producer(q, barrier, s, iters)); + pool.execute(new Consumer(q, barrier, s, iters)); } barrier.await(); barrier.await();