--- jsr166/src/test/loops/NoopSpinLockLoops.java 2005/05/02 19:19:38 1.1 +++ jsr166/src/test/loops/NoopSpinLockLoops.java 2016/12/31 18:54:28 1.9 @@ -1,17 +1,14 @@ /* - * @test - * @synopsis multiple threads using a single builtin lock - */ -/* * Written by 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. + * Expert Group and released to the public domain, as explained at + * http://creativecommons.org/publicdomain/zero/1.0/ */ -import java.util.concurrent.*; -import java.util.concurrent.locks.*; -import java.util.concurrent.atomic.*; -import java.util.*; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; public final class NoopSpinLockLoops { static final ExecutorService pool = Executors.newCachedThreadPool(); @@ -21,23 +18,22 @@ public final class NoopSpinLockLoops { public static void main(String[] args) throws Exception { int maxThreads = 100; - if (args.length > 0) + if (args.length > 0) maxThreads = Integer.parseInt(args[0]); new ReentrantLockLoop(1).test(); new ReentrantLockLoop(1).test(); print = true; - int k = 1; - for (int i = 1; i <= maxThreads;) { + for (int k = 1, i = 1; i <= maxThreads;) { System.out.print("Threads: " + i); new ReentrantLockLoop(i).test(); Thread.sleep(100); if (i == k) { k = i << 1; i = i + (i >>> 1); - } - else + } + else i = k; } pool.shutdown(); @@ -57,15 +53,15 @@ public final class NoopSpinLockLoops { } final void test() throws Exception { - for (int i = 0; i < nthreads; ++i) + for (int i = 0; i < nthreads; ++i) pool.execute(this); barrier.await(); barrier.await(); if (print) { long time = timer.getTime(); - long tpi = time / ((long)iters * nthreads); + long tpi = time / ((long) iters * nthreads); System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock"); - double secs = (double)(time) / 1000000000.0; + double secs = (double) time / 1000000000.0; System.out.println("\t " + secs + "s run time"); } @@ -77,12 +73,12 @@ public final class NoopSpinLockLoops { public final void run() { final AtomicInteger lock = this.spinlock; try { - barrier.await(); + barrier.await(); int sum = v + 1; int x = sum + 1; int n = iters; while (n-- > 0) { - while (!lock.compareAndSet(0, 1)) ; + do {} while (!lock.compareAndSet(0, 1)); x = LoopHelpers.compute4(x); lock.set(0); if ((x += readBarrier) == 0) @@ -93,11 +89,10 @@ public final class NoopSpinLockLoops { barrier.await(); result += sum; } - catch (Exception ie) { - return; + catch (Exception ie) { + return; } } } } -