--- jsr166/src/test/loops/TieredPhaserLoops.java 2009/11/02 20:23:53 1.2 +++ jsr166/src/test/loops/TieredPhaserLoops.java 2014/12/18 18:43:22 1.8 @@ -1,17 +1,16 @@ /* * 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.*; -/* +/** * Based loosely on Java Grande Forum barrierBench */ - public class TieredPhaserLoops { static final int NCPUS = Runtime.getRuntime().availableProcessors(); static final ExecutorService pool = Executors.newCachedThreadPool(); @@ -23,21 +22,18 @@ public class TieredPhaserLoops { static int tasksPerPhaser = Math.max(NCPUS / 8, 4); static void build(Runnable[] actions, int sz, int lo, int hi, Phaser b) { - int step = (hi - lo) / tasksPerPhaser; - if (step > 1) { - int i = lo; - while (i < hi) { - int r = Math.min(i + step, hi); - build(actions, sz, i, r, new Phaser(b)); - i = r; + if (hi - lo > tasksPerPhaser) { + for (int i = lo; i < hi; i += tasksPerPhaser) { + int j = Math.min(i + tasksPerPhaser, hi); + build(actions, sz, i, j, new Phaser(b)); } - } - else { + } else { for (int i = lo; i < hi; ++i) actions[i] = new PhaserAction(i, b, sz); } } + static final class PhaserAction implements Runnable { final int id; final int size; @@ -53,7 +49,7 @@ public class TieredPhaserLoops { public void run() { int n = size; Phaser b = phaser; - for(int i = 0; i < n; ++i) + for (int i = 0; i < n; ++i) b.arriveAndAwaitAdvance(); } } @@ -73,7 +69,7 @@ public class TieredPhaserLoops { Runnable[] actions = new Runnable [k]; build(actions, size, 0, k, new Phaser()); - Future[] futures = new Future[k]; + Future[] futures = new Future[k]; for (int i = 0; i < k; ++i) { futures[i] = pool.submit(actions[i]); }