--- jsr166/src/test/loops/SpinningTieredPhaserLoops.java 2009/11/01 21:28:57 1.1 +++ jsr166/src/test/loops/SpinningTieredPhaserLoops.java 2011/03/15 19:47:06 1.5 @@ -1,7 +1,7 @@ /* * 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.*; @@ -21,19 +21,15 @@ public class SpinningTieredPhaserLoops { static final long NPS = (1000L * 1000 * 1000); static int tasksPerPhaser = Math.max(NCPUS / 4, 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 { - for (int i = lo; i < hi; ++i) + } else { + for (int i = lo; i < hi; ++i) actions[i] = new PhaserAction(i, b, sz); } } @@ -53,7 +49,7 @@ public class SpinningTieredPhaserLoops { public void run() { int n = size; Phaser b = phaser; - for(int i = 0; i < n; ++i) { + for (int i = 0; i < n; ++i) { int p = b.arrive(); while (b.getPhase() == p) { if ((ThreadLocalRandom.current().nextInt() & 127) == 0) @@ -69,13 +65,13 @@ public class SpinningTieredPhaserLoops { nthreads = Integer.parseInt(args[0]); if (args.length > 1) tasksPerPhaser = Integer.parseInt(args[1]); - + System.out.printf("Max %d Threads, %d tasks per phaser\n", nthreads, tasksPerPhaser); for (int k = 2; k <= nthreads; k *= 2) { for (int size = FIRST_SIZE; size <= LAST_SIZE; size *= 10) { long startTime = System.nanoTime(); - + Runnable[] actions = new Runnable [k]; build(actions, size, 0, k, new Phaser()); Future[] futures = new Future[k]; @@ -93,7 +89,5 @@ public class SpinningTieredPhaserLoops { } pool.shutdown(); } - -} - +}