--- jsr166/src/test/loops/ExchangeLoops.java 2006/02/13 12:39:23 1.2 +++ jsr166/src/test/loops/ExchangeLoops.java 2015/01/02 16:57:15 1.9 @@ -1,8 +1,7 @@ /* * Written by Bill Scherer and Doug Lea with assistance from members - * of JCP JSR-166 Expert Group and released to the public domain. Use, - * modify, and redistribute this code in any way without - * acknowledgement. + * of JCP JSR-166 Expert Group and released to the public domain, as + * explained at http://creativecommons.org/publicdomain/zero/1.0/ */ import java.util.concurrent.*; @@ -13,7 +12,7 @@ public class ExchangeLoops { static final int NCPUS = Runtime.getRuntime().availableProcessors(); static final int DEFAULT_THREADS = NCPUS + 2; - static final long DEFAULT_TRIAL_MILLIS = 10000; + static final long DEFAULT_TRIAL_MILLIS = 10000; public static void main(String[] args) throws Exception { int maxThreads = DEFAULT_THREADS; @@ -28,20 +27,20 @@ public class ExchangeLoops { trialMillis = Integer.parseInt(args[argc]); else if (option.equals("-r")) nReps = Integer.parseInt(args[argc]); - else + else maxThreads = Integer.parseInt(option); argc++; } - // Display runtime parameters - System.out.print("ExchangeTest"); - System.out.print(" -t " + trialMillis); + // Display runtime parameters + System.out.print("ExchangeTest"); + System.out.print(" -t " + trialMillis); System.out.print(" -r " + nReps); - System.out.print(" max threads " + maxThreads); - System.out.println(); + System.out.print(" max threads " + maxThreads); + System.out.println(); long warmupTime = 2000; long sleepTime = 100; - int nw = maxThreads >= 3? 3 : 2; + int nw = (maxThreads >= 3) ? 3 : 2; System.out.println("Warmups.."); oneRun(3, warmupTime); @@ -79,7 +78,6 @@ public class ExchangeLoops { static void oneRun(int nThreads, long trialMillis) throws Exception { System.out.printf("%4d threads", nThreads); - System.out.printf("%9dms", trialMillis); Exchanger x = new Exchanger(); Runner[] runners = new Runner[nThreads]; Thread[] threads = new Thread[nThreads]; @@ -98,10 +96,10 @@ public class ExchangeLoops { threads[i].start(); } Thread.sleep(trialMillis); - for (int i = 0; i < nThreads; ++i) + for (int i = 0; i < nThreads; ++i) threads[i].interrupt(); long elapsed = System.nanoTime() - startTime; - for (int i = 0; i < nThreads; ++i) + for (int i = 0; i < nThreads; ++i) threads[i].join(); int iters = 1; // System.out.println(); @@ -112,12 +110,13 @@ public class ExchangeLoops { } long rate = iters * 1000L * 1000L * 1000L / elapsed; long npt = elapsed / iters; + System.out.printf("%9dms", elapsed / (1000L * 1000L)); System.out.printf("%9d it/s ", rate); System.out.printf("%9d ns/it", npt); System.out.println(); // x.printStats(); } - + static final class Runner implements Runnable { final Exchanger exchanger; final Object mine = new Integer(2688); @@ -142,4 +141,3 @@ public class ExchangeLoops { } } } -