--- jsr166/src/test/loops/CheckedLockLoops.java 2005/05/02 19:19:38 1.1 +++ jsr166/src/test/loops/CheckedLockLoops.java 2010/09/16 03:57:13 1.6 @@ -1,4 +1,9 @@ /* + * 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 + */ +/* * @test * @summary basic safety and liveness of ReentrantLocks, and other locks based on them */ @@ -16,7 +21,7 @@ public final class CheckedLockLoops { public static void main(String[] args) throws Exception { int maxThreads = 100; int iters = 2000000; - if (args.length > 0) + if (args.length > 0) maxThreads = Integer.parseInt(args[0]); rng.setSeed(3122688L); warmup(iters); @@ -33,11 +38,11 @@ public final class CheckedLockLoops { if (i == k) { k = i << 1; i = i + (i >>> 1); - } - else + } + else i = k; } - } + } static void warmup(int iters) throws Exception { print = false; @@ -47,14 +52,14 @@ public final class CheckedLockLoops { } static void oneTest(int nthreads, int iters) throws Exception { - int fairIters = (nthreads <= 1)? iters : iters/20; + int fairIters = (nthreads <= 1) ? iters : iters/20; int v = rng.next(); if (print) System.out.print("NoLock (1 thread) "); new NoLockLoop().test(v, 1, iters * nthreads); Thread.sleep(10); - + if (print) System.out.print("ReentrantLock "); new ReentrantLockLoop().test(v, nthreads, iters); @@ -86,7 +91,7 @@ public final class CheckedLockLoops { System.out.print("Semaphore "); new SemaphoreLoop().test(v, nthreads, iters); Thread.sleep(10); - + if (print) System.out.print("FairSemaphore "); new FairSemaphoreLoop().test(v, nthreads, fairIters); @@ -101,12 +106,12 @@ public final class CheckedLockLoops { System.out.print("FairRWriteLock "); new FairReentrantWriteLockLoop().test(v, nthreads, fairIters); Thread.sleep(10); - + if (print) System.out.print("ReentrantReadWriteLock"); new ReentrantReadWriteLockLoop().test(v, nthreads, iters); Thread.sleep(10); - + if (print) System.out.print("FairRReadWriteLock "); new FairReentrantReadWriteLockLoop().test(v, nthreads, fairIters); @@ -130,7 +135,7 @@ public final class CheckedLockLoops { final int getValue() { int v = value; - if (checkValue != ~(v ^ 0xAAAAAAAA)) + if (checkValue != ~(v ^ 0xAAAAAAAA)) ++failures; return v; } @@ -139,7 +144,7 @@ public final class CheckedLockLoops { setValue(initialValue); this.iters = iters; barrier = new CyclicBarrier(nthreads+1, timer); - for (int i = 0; i < nthreads; ++i) + for (int i = 0; i < nthreads; ++i) pool.execute(this); barrier.await(); barrier.await(); @@ -159,12 +164,12 @@ public final class CheckedLockLoops { abstract int loop(int n); public final void run() { try { - barrier.await(); + barrier.await(); result += loop(iters); barrier.await(); } - catch (Exception ie) { - return; + catch (Exception ie) { + return; } } @@ -174,7 +179,7 @@ public final class CheckedLockLoops { private volatile int readBarrier; final int loop(int n) { int sum = 0; - int x = 0;; + int x = 0; while (n-- > 0) { int r1 = readBarrier; x = setValue(LoopHelpers.compute1(getValue())); @@ -190,9 +195,9 @@ public final class CheckedLockLoops { private static class BuiltinLockLoop extends LockLoop { final int loop(int n) { int sum = 0; - int x = 0;; + int x = 0; while (n-- > 0) { - synchronized(this) { + synchronized (this) { x = setValue(LoopHelpers.compute1(getValue())); } sum += LoopHelpers.compute2(x);