--- jsr166/src/test/loops/TimeoutProducerConsumerLoops.java 2009/11/14 20:58:11 1.5 +++ jsr166/src/test/loops/TimeoutProducerConsumerLoops.java 2015/01/15 18:34:19 1.12 @@ -1,12 +1,11 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ import java.util.concurrent.*; - public class TimeoutProducerConsumerLoops { static final int NCPUS = Runtime.getRuntime().availableProcessors(); static final ExecutorService pool = Executors.newCachedThreadPool(); @@ -17,11 +16,11 @@ public class TimeoutProducerConsumerLoop static final int POOL_MASK = POOL_SIZE-1; static final Integer[] intPool = new Integer[POOL_SIZE]; static { - for (int i = 0; i < POOL_SIZE; ++i) + for (int i = 0; i < POOL_SIZE; ++i) intPool[i] = Integer.valueOf(i); } - // max lag between a producer and consumer to avoid + // 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; @@ -47,24 +46,23 @@ public class TimeoutProducerConsumerLoop int maxPairs = NCPUS * 3 / 2; int iters = 1000000; - if (args.length > 0) + if (args.length > 0) maxPairs = Integer.parseInt(args[0]); print = true; - int k = 1; - for (int i = 1; i <= maxPairs;) { + for (int k = 1, i = 1; i <= maxPairs;) { System.out.println("Pairs:" + i); oneTest(i, iters); Thread.sleep(100); if (i == k) { k = i << 1; i = i + (i >>> 1); - } - else + } + else i = k; } pool.shutdown(); - } + } static void oneTest(int n, int iters) throws Exception { if (print) @@ -108,14 +106,14 @@ public class TimeoutProducerConsumerLoop oneRun(new ArrayBlockingQueue(POOL_SIZE, true), n, iters/16); } - - static abstract class Stage implements Runnable { + + abstract static class Stage implements Runnable { final int iters; final BlockingQueue queue; final CyclicBarrier barrier; final Phaser lagPhaser; - Stage (BlockingQueue q, CyclicBarrier b, Phaser s, int iters) { - queue = q; + Stage(BlockingQueue q, CyclicBarrier b, Phaser s, int iters) { + queue = q; barrier = b; lagPhaser = s; this.iters = iters; @@ -151,15 +149,15 @@ public class TimeoutProducerConsumerLoop addProducerSum(s); barrier.await(); } - catch (Exception ie) { - ie.printStackTrace(); - return; + catch (Exception ie) { + ie.printStackTrace(); + return; } } } static class Consumer extends Stage { - Consumer(BlockingQueue q, CyclicBarrier b, Phaser s, int iters) { + Consumer(BlockingQueue q, CyclicBarrier b, Phaser s, int iters) { super(q, b, s, iters); } @@ -171,7 +169,7 @@ public class TimeoutProducerConsumerLoop int i = 0; long timeout = 1000; while (i < iters) { - Integer e = queue.poll(timeout, + Integer e = queue.poll(timeout, TimeUnit.NANOSECONDS); if (e != null) { l = LoopHelpers.compute4(e.intValue()); @@ -188,9 +186,9 @@ public class TimeoutProducerConsumerLoop addConsumerSum(s); barrier.await(); } - catch (Exception ie) { - ie.printStackTrace(); - return; + catch (Exception ie) { + ie.printStackTrace(); + return; } } @@ -216,12 +214,12 @@ public class TimeoutProducerConsumerLoop static final class LTQasSQ extends LinkedTransferQueue { LTQasSQ() { super(); } public void put(T x) { - try { super.transfer(x); + try { super.transfer(x); } catch (InterruptedException ex) { throw new Error(); } } public boolean offer(T x, long timeout, TimeUnit unit) { - return super.offer(x, timeout, unit); + return super.offer(x, timeout, unit); } }