--- jsr166/src/test/loops/CASLoops.java 2007/06/24 21:28:19 1.3 +++ jsr166/src/test/loops/CASLoops.java 2016/12/31 19:10:25 1.11 @@ -1,6 +1,6 @@ /* * Written by Doug Lea and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ /* @@ -26,12 +26,11 @@ * if this program is called with (any) second argument */ - +import java.util.concurrent.CyclicBarrier; import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; -import java.util.concurrent.*; -import java.util.concurrent.locks.*; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.AbstractQueuedSynchronizer; public class CASLoops { @@ -43,10 +42,10 @@ public class CASLoops { static boolean includeLocks = false; public static void main(String[] args) throws Exception { - if (args.length > 0) + if (args.length > 0) maxThreads = Integer.parseInt(args[0]); - loopIters = new long[maxThreads+1]; + loopIters = new long[maxThreads+1]; if (args.length > 1) includeLocks = true; @@ -58,7 +57,7 @@ public class CASLoops { System.out.print("."); } - for (int i = 1; i <= maxThreads; ++i) + for (int i = 1; i <= maxThreads; ++i) loopIters[i] = 0; for (int j = 0; j < 2; ++j) { @@ -69,7 +68,7 @@ public class CASLoops { } } - for (int i = 1; i <= maxThreads; ++i) + for (int i = 1; i <= maxThreads; ++i) loopIters[i] = 0; for (int j = 0; j < TRIALS; ++j) { @@ -111,11 +110,10 @@ public class CASLoops { static final class UpdaterAtomicInteger { volatile int value; - static final AtomicIntegerFieldUpdater + static final AtomicIntegerFieldUpdater valueUpdater = AtomicIntegerFieldUpdater.newUpdater (UpdaterAtomicInteger.class, "value"); - UpdaterAtomicInteger() {} int get() { return value; @@ -161,7 +159,6 @@ public class CASLoops { synchronized void set(int val) { value = val; } } - static final class LockedInteger extends AbstractQueuedSynchronizer { int value; LockedInteger() {} @@ -191,17 +188,17 @@ public class CASLoops { unlock(); } } - void set(int val) { - lock(); + void set(int val) { + lock(); try { - value = val; + value = val; } finally { unlock(); } } } - // All these versions are copy-paste-hacked to avoid + // All these versions are copy-paste-hacked to avoid // contamination with virtual call resolution etc. // Use fixed-length unrollable inner loops to reduce safepoint checks @@ -220,7 +217,7 @@ public class CASLoops { public void run() { try { - barrier.await(); + barrier.await(); long i = iters; int y = 0; int succ = 0; @@ -238,8 +235,8 @@ public class CASLoops { successes.getAndAdd(succ); barrier.await(); } - catch (Exception ie) { - return; + catch (Exception ie) { + return; } } } @@ -257,7 +254,7 @@ public class CASLoops { public void run() { try { - barrier.await(); + barrier.await(); long i = iters; int y = 0; int succ = 0; @@ -275,8 +272,8 @@ public class CASLoops { successes.getAndAdd(succ); barrier.await(); } - catch (Exception ie) { - return; + catch (Exception ie) { + return; } } } @@ -294,7 +291,7 @@ public class CASLoops { public void run() { try { - barrier.await(); + barrier.await(); long i = iters; int y = 0; int succ = 0; @@ -312,8 +309,8 @@ public class CASLoops { successes.getAndAdd(succ); barrier.await(); } - catch (Exception ie) { - return; + catch (Exception ie) { + return; } } } @@ -331,7 +328,7 @@ public class CASLoops { public void run() { try { - barrier.await(); + barrier.await(); long i = iters; int y = 0; int succ = 0; @@ -349,8 +346,8 @@ public class CASLoops { successes.getAndAdd(succ); barrier.await(); } - catch (Exception ie) { - return; + catch (Exception ie) { + return; } } } @@ -368,7 +365,7 @@ public class CASLoops { public void run() { try { - barrier.await(); + barrier.await(); long i = iters; int y = 0; int succ = 0; @@ -386,8 +383,8 @@ public class CASLoops { successes.getAndAdd(succ); barrier.await(); } - catch (Exception ie) { - return; + catch (Exception ie) { + return; } } } @@ -405,7 +402,7 @@ public class CASLoops { public void run() { try { - barrier.await(); + barrier.await(); long i = iters; int y = 0; int succ = 0; @@ -423,8 +420,8 @@ public class CASLoops { successes.getAndAdd(succ); barrier.await(); } - catch (Exception ie) { - return; + catch (Exception ie) { + return; } } } @@ -444,7 +441,7 @@ public class CASLoops { public void run() { try { - barrier.await(); + barrier.await(); long iters = 0; int y = 0; int succ = 0; @@ -467,8 +464,8 @@ public class CASLoops { successes.getAndAdd(succ); barrier.await(); } - catch (Exception ie) { - return; + catch (Exception ie) { + return; } } } @@ -479,7 +476,7 @@ public class CASLoops { CyclicBarrier b = new CyclicBarrier(n+1); totalIters.set(0); NonAtomicInteger a = new NonAtomicInteger(); - for (int j = 0; j < n; ++j) + for (int j = 0; j < n; ++j) new Thread(new NACalibrationLoop(endTime, a, b)).start(); b.await(); b.await(); @@ -493,7 +490,7 @@ public class CASLoops { LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier b = new CyclicBarrier(n+1, timer); NonAtomicInteger a = new NonAtomicInteger(); - for (int j = 0; j < n; ++j) + for (int j = 0; j < n; ++j) new Thread(new NonAtomicLoop(iters, a, b)).start(); b.await(); b.await(); @@ -505,7 +502,7 @@ public class CASLoops { LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier b = new CyclicBarrier(n+1, timer); UpdaterAtomicInteger a = new UpdaterAtomicInteger(); - for (int j = 0; j < n; ++j) + for (int j = 0; j < n; ++j) new Thread(new UpdaterAtomicLoop(iters, a, b)).start(); b.await(); b.await(); @@ -517,7 +514,7 @@ public class CASLoops { LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier b = new CyclicBarrier(n+1, timer); AtomicInteger a = new AtomicInteger(); - for (int j = 0; j < n; ++j) + for (int j = 0; j < n; ++j) new Thread(new AtomicLoop(iters, a, b)).start(); b.await(); b.await(); @@ -529,7 +526,7 @@ public class CASLoops { LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier b = new CyclicBarrier(n+1, timer); VolatileInteger a = new VolatileInteger(); - for (int j = 0; j < n; ++j) + for (int j = 0; j < n; ++j) new Thread(new VolatileLoop(iters, a, b)).start(); b.await(); b.await(); @@ -537,12 +534,11 @@ public class CASLoops { return timer.getTime(); } - static long runSynched(int n, long iters) throws Exception { LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier b = new CyclicBarrier(n+1, timer); SynchedInteger a = new SynchedInteger(); - for (int j = 0; j < n; ++j) + for (int j = 0; j < n; ++j) new Thread(new SynchedLoop(iters, a, b)).start(); b.await(); b.await(); @@ -554,7 +550,7 @@ public class CASLoops { LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier b = new CyclicBarrier(n+1, timer); LockedInteger a = new LockedInteger(); - for (int j = 0; j < n; ++j) + for (int j = 0; j < n; ++j) new Thread(new LockedLoop(iters, a, b)).start(); b.await(); b.await(); @@ -562,24 +558,23 @@ public class CASLoops { return timer.getTime(); } - static void report(String tag, long runtime, long basetime, + static void report(String tag, long runtime, long basetime, int nthreads, long iters) { System.out.print(tag); long t = (runtime - basetime) / iters; if (nthreads > NCPUS) t = t * NCPUS / nthreads; System.out.print(LoopHelpers.rightJustify(t)); - double secs = (double)(runtime) / 1000000000.0; + double secs = (double) runtime / 1000000000.0; System.out.println("\t " + secs + "s run time"); } - static void oneRun(int i, long iters, boolean print) throws Exception { - if (print) - System.out.println("threads : " + i + - " base iters per thread per run : " + + if (print) + System.out.println("threads : " + i + + " base iters per thread per run : " + LoopHelpers.rightJustify(loopIters[i])); - long ntime = runNonAtomic(i, iters); + long ntime = runNonAtomic(i, iters); if (print) report("Base : ", ntime, ntime, i, iters); Thread.sleep(100L); @@ -602,10 +597,8 @@ public class CASLoops { report("Mutex : ", mtime, ntime, i, iters); Thread.sleep(100L); long stime = runSynched(i, iters); - if (print) + if (print) report("Synchronized: ", stime, ntime, i, iters); Thread.sleep(100L); } - - }