--- jsr166/src/test/loops/ConcurrentDequeLoops.java 2007/02/19 00:46:06 1.2 +++ jsr166/src/test/loops/ConcurrentDequeLoops.java 2016/12/31 19:02:43 1.14 @@ -1,13 +1,17 @@ /* * 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 java.util.concurrent.atomic.*; -import java.util.concurrent.locks.*; +import java.util.ArrayList; +import java.util.Deque; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicInteger; public class ConcurrentDequeLoops { static final ExecutorService pool = Executors.newCachedThreadPool(); @@ -18,18 +22,18 @@ public class ConcurrentDequeLoops { int maxStages = 8; int items = 1000000; - Class klass = null; + Class klass = null; if (args.length > 0) { try { klass = Class.forName(args[0]); - } catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { throw new RuntimeException("Class " + args[0] + " not found."); } } - else + else throw new Error(); - if (args.length > 1) + if (args.length > 1) maxStages = Integer.parseInt(args[1]); System.out.print("Class: " + klass.getName()); @@ -43,26 +47,25 @@ public class ConcurrentDequeLoops { Thread.sleep(100); print = true; - int k = 1; - for (int i = 1; i <= maxStages;) { + for (int k = 1, i = 1; i <= maxStages;) { oneRun(klass, i, items); if (i == k) { k = i << 1; i = i + (i >>> 1); - } - else + } + else i = k; } pool.shutdown(); - } + } static class Stage implements Callable { final Deque queue; final CyclicBarrier barrier; final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); int items; - Stage (Deque q, CyclicBarrier b, int items) { - queue = q; + Stage(Deque q, CyclicBarrier b, int items) { + queue = q; barrier = b; this.items = items; } @@ -72,7 +75,7 @@ public class ConcurrentDequeLoops { // transform it, and put back in. try { barrier.await(); - int l = (int)System.nanoTime(); + int l = (int) System.nanoTime(); int takes = 0; for (;;) { Integer item; @@ -100,7 +103,7 @@ public class ConcurrentDequeLoops { else queue.addLast(res); } - else { // spinwait + else { // spinwait for (int k = 1 + (l & 15); k != 0; --k) l = LoopHelpers.compute1(LoopHelpers.compute2(l)); if ((l & 3) == 3) { @@ -110,20 +113,21 @@ public class ConcurrentDequeLoops { } return new Integer(l); } - catch (Exception ie) { + catch (Exception ie) { ie.printStackTrace(); throw new Error("Call loop failed"); } } } - static void oneRun(Class klass, int n, int items) throws Exception { - Deque q = (Deque)klass.newInstance(); + static void oneRun(Class klass, int n, int items) throws Exception { + Deque q = + (Deque) klass.getConstructor().newInstance(); LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier barrier = new CyclicBarrier(n + 1, timer); totalItems = new AtomicInteger(n * items); ArrayList> results = new ArrayList>(n); - for (int i = 0; i < n; ++i) + for (int i = 0; i < n; ++i) results.add(pool.submit(new Stage(q, barrier, items))); if (print) @@ -141,6 +145,5 @@ public class ConcurrentDequeLoops { System.out.println(LoopHelpers.rightJustify(time / (items * n)) + " ns per item"); if (total == 0) // avoid overoptimization System.out.println("useless result: " + total); - } }