--- jsr166/src/test/loops/MultipleProducersSingleConsumerLoops.java 2009/10/23 19:57:06 1.4 +++ jsr166/src/test/loops/MultipleProducersSingleConsumerLoops.java 2016/12/31 19:50:56 1.13 @@ -1,16 +1,24 @@ /* * 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.*; -import java.util.concurrent.*; -//import jsr166y.*; +import java.util.Random; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.LinkedTransferQueue; +import java.util.concurrent.Phaser; +import java.util.concurrent.PriorityBlockingQueue; +import java.util.concurrent.SynchronousQueue; public class MultipleProducersSingleConsumerLoops { static final int NCPUS = Runtime.getRuntime().availableProcessors(); - static final Random rng = new Random(); static final ExecutorService pool = Executors.newCachedThreadPool(); static boolean print = false; static int producerSum; @@ -34,14 +42,14 @@ public class MultipleProducersSingleCons 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); } // Number of puts by producers or takes by consumers static final int ITERS = 1 << 20; - // 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. static final int LAG = (1 << 12); static final int LAG_MASK = LAG - 1; @@ -49,20 +57,19 @@ public class MultipleProducersSingleCons public static void main(String[] args) throws Exception { int maxn = 12; // NCPUS * 3 / 2; - if (args.length > 0) + if (args.length > 0) maxn = Integer.parseInt(args[0]); warmup(); print = true; - int k = 1; - for (int i = 1; i <= maxn;) { + for (int k = 1, i = 1; i <= maxn;) { System.out.println("Producers:" + i); oneTest(i, ITERS); if (i == k) { k = i << 1; i = i + (i >>> 1); - } - else + } + else i = k; } pool.shutdown(); @@ -73,14 +80,14 @@ public class MultipleProducersSingleCons System.out.print("Warmup "); int it = 2000; for (int j = 5; j > 0; --j) { - oneTest(j, it); + oneTest(j, it); System.out.print("."); it += 1000; } System.gc(); it = 20000; for (int j = 5; j > 0; --j) { - oneTest(j, it); + oneTest(j, it); System.out.print("."); it += 10000; } @@ -110,7 +117,7 @@ public class MultipleProducersSingleCons if (print) System.out.print("LinkedBlockingDeque "); oneRun(new LinkedBlockingDeque(), n, iters); - + Thread.sleep(100); // System.gc(); if (print) System.out.print("ArrayBlockingQueue "); @@ -121,7 +128,6 @@ public class MultipleProducersSingleCons System.out.print("SynchronousQueue "); oneRun(new SynchronousQueue(), n, iters); - Thread.sleep(100); // System.gc(); if (print) System.out.print("SynchronousQueue(fair) "); @@ -136,7 +142,7 @@ public class MultipleProducersSingleCons if (print) System.out.print("LinkedTransferQueue(half)"); oneRun(new HalfSyncLTQ(), n, iters); - + Thread.sleep(100); // System.gc(); if (print) System.out.print("PriorityBlockingQueue "); @@ -146,19 +152,17 @@ public class MultipleProducersSingleCons if (print) System.out.print("ArrayBlockingQueue(fair)"); oneRun(new ArrayBlockingQueue(POOL_SIZE, true), n, fairIters); - - } - - static abstract class Stage implements Runnable { + + abstract static class Stage implements Runnable { final int iters; final BlockingQueue queue; final CyclicBarrier barrier; final Phaser lagPhaser; final int lag; - Stage (BlockingQueue q, CyclicBarrier b, Phaser s, - int iters, int lag) { - queue = q; + Stage(BlockingQueue q, CyclicBarrier b, Phaser s, + int iters, int lag) { + queue = q; barrier = b; lagPhaser = s; this.iters = iters; @@ -192,16 +196,16 @@ public class MultipleProducersSingleCons addProducerSum(ps); 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, int lag) { + int iters, int lag) { super(q, b, s, iters, lag); } @@ -222,15 +226,14 @@ public class MultipleProducersSingleCons addConsumerSum(cs); barrier.await(); } - catch (Exception ie) { - ie.printStackTrace(); - return; + catch (Exception ie) { + ie.printStackTrace(); + return; } } } - static void oneRun(BlockingQueue q, int n, int iters) throws Exception { LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); @@ -247,11 +250,11 @@ public class MultipleProducersSingleCons if (print) System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * (n + 1))) + " ns per transfer"); } - + 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(); } } } @@ -263,12 +266,11 @@ public class MultipleProducersSingleCons if ((++calls & 1) == 0) super.put(x); else { - try { super.transfer(x); - } catch (InterruptedException ex) { - throw new Error(); + try { super.transfer(x); + } catch (InterruptedException ex) { + throw new Error(); } } } } } -