--- jsr166/src/test/loops/UncheckedLockLoops.java 2005/05/09 19:33:30 1.2 +++ jsr166/src/test/loops/UncheckedLockLoops.java 2015/01/15 18:34:19 1.11 @@ -1,16 +1,16 @@ /* * 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/ */ /* * @test * @summary basic safety and liveness of ReentrantLocks, and other locks based on them */ +import java.util.*; import java.util.concurrent.*; import java.util.concurrent.locks.*; -import java.util.*; public final class UncheckedLockLoops { static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); @@ -21,7 +21,7 @@ public final class UncheckedLockLoops { int maxThreads = 100; int iters = 10000000; - if (args.length > 0) + if (args.length > 0) maxThreads = Integer.parseInt(args[0]); rng.setSeed(3122688L); @@ -53,14 +53,14 @@ public final class UncheckedLockLoops { } 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); @@ -93,7 +93,7 @@ public final class UncheckedLockLoops { 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); @@ -108,12 +108,12 @@ public final class UncheckedLockLoops { 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); @@ -121,7 +121,7 @@ public final class UncheckedLockLoops { } - static abstract class LockLoop implements Runnable { + abstract static class LockLoop implements Runnable { int value; int checkValue; int iters; @@ -138,7 +138,7 @@ public final class UncheckedLockLoops { final int getValue() { int v = value; - if (checkValue != ~(v ^ 0xAAAAAAAA)) + if (checkValue != ~(v ^ 0xAAAAAAAA)) ++failures; return v; } @@ -147,7 +147,7 @@ public final class UncheckedLockLoops { 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) new Thread(this).start(); barrier.await(); barrier.await(); @@ -155,7 +155,7 @@ public final class UncheckedLockLoops { if (print) { long tpi = time / (iters * nthreads); System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update"); - // double secs = (double)(time) / 1000000000.0; + // double secs = (double) time / 1000000000.0; // System.out.print("\t " + secs + "s run time"); System.out.println(); } @@ -168,12 +168,12 @@ public final class UncheckedLockLoops { 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; } } @@ -183,7 +183,7 @@ public final class UncheckedLockLoops { 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())); @@ -199,9 +199,9 @@ public final class UncheckedLockLoops { 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); @@ -211,7 +211,7 @@ public final class UncheckedLockLoops { } private static class ReentrantLockLoop extends LockLoop { - final private ReentrantLock lock = new ReentrantLock(); + private final ReentrantLock lock = new ReentrantLock(); final int loop(int n) { final ReentrantLock lock = this.lock; int sum = 0; @@ -231,7 +231,7 @@ public final class UncheckedLockLoops { } private static class MutexLoop extends LockLoop { - final private Mutex lock = new Mutex(); + private final Mutex lock = new Mutex(); final int loop(int n) { final Mutex lock = this.lock; int sum = 0; @@ -251,7 +251,7 @@ public final class UncheckedLockLoops { } private static class LongMutexLoop extends LockLoop { - final private LongMutex lock = new LongMutex(); + private final LongMutex lock = new LongMutex(); final int loop(int n) { final LongMutex lock = this.lock; int sum = 0; @@ -271,7 +271,7 @@ public final class UncheckedLockLoops { } private static class FairReentrantLockLoop extends LockLoop { - final private ReentrantLock lock = new ReentrantLock(true); + private final ReentrantLock lock = new ReentrantLock(true); final int loop(int n) { final ReentrantLock lock = this.lock; int sum = 0; @@ -291,7 +291,7 @@ public final class UncheckedLockLoops { } private static class ReentrantWriteLockLoop extends LockLoop { - final private Lock lock = new ReentrantReadWriteLock().writeLock(); + private final Lock lock = new ReentrantReadWriteLock().writeLock(); final int loop(int n) { final Lock lock = this.lock; int sum = 0; @@ -331,7 +331,7 @@ public final class UncheckedLockLoops { } private static class SemaphoreLoop extends LockLoop { - final private Semaphore sem = new Semaphore(1, false); + private final Semaphore sem = new Semaphore(1, false); final int loop(int n) { final Semaphore sem = this.sem; int sum = 0; @@ -350,7 +350,7 @@ public final class UncheckedLockLoops { } } private static class FairSemaphoreLoop extends LockLoop { - final private Semaphore sem = new Semaphore(1, true); + private final Semaphore sem = new Semaphore(1, true); final int loop(int n) { final Semaphore sem = this.sem; int sum = 0; @@ -370,7 +370,7 @@ public final class UncheckedLockLoops { } private static class ReentrantReadWriteLockLoop extends LockLoop { - final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final int loop(int n) { final Lock rlock = lock.readLock(); final Lock wlock = lock.writeLock(); @@ -403,9 +403,8 @@ public final class UncheckedLockLoops { } - private static class FairReentrantReadWriteLockLoop extends LockLoop { - final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); final int loop(int n) { final Lock rlock = lock.readLock(); final Lock wlock = lock.writeLock();