--- jsr166/src/test/loops/LockLoops.java 2009/10/29 23:09:07 1.3 +++ jsr166/src/test/loops/LockLoops.java 2016/06/09 15:28:19 1.15 @@ -1,15 +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/ */ + /* - * A simple test program. Feel free to play. + * Simple benchmark comparing various locking techniques. */ +import java.util.*; import java.util.concurrent.*; import java.util.concurrent.locks.*; -import java.util.*; public final class LockLoops { static final ExecutorService pool = Executors.newCachedThreadPool(); @@ -18,7 +19,8 @@ public final class LockLoops { static boolean doBuiltin = true; static boolean doReadWrite = true; static boolean doSemaphore = true; - static boolean doFair = true; + static boolean doStampedLock = true; + static boolean doFair = true; public static void main(String[] args) throws Exception { int maxThreads = 100; @@ -90,10 +92,14 @@ public final class LockLoops { Thread.sleep(10); if (print) + System.out.print("ReentrantReadLock "); + new ReentrantReadLockLoop().test(v, nthreads, iters); + Thread.sleep(10); + + if (print) System.out.print("ReentrantReadWriteLock"); new ReentrantReadWriteLockLoop().test(v, nthreads, iters); Thread.sleep(10); - } if (doSemaphore) { @@ -106,7 +112,6 @@ public final class LockLoops { System.out.print("FairSemaphore "); new FairSemaphoreLoop().test(v, nthreads, iters); Thread.sleep(10); - } if (doFair) { @@ -126,12 +131,32 @@ public final class LockLoops { new FairReentrantReadWriteLockLoop().test(v, nthreads, iters); Thread.sleep(10); } - } + if (doStampedLock) { + if (print) + System.out.print("StampedLockWrite "); + new StampedLockWriteLoop().test(v, nthreads, iters); + Thread.sleep(10); + + if (print) + System.out.print("StampedLockRead "); + new StampedLockReadLoop().test(v, nthreads, iters); + Thread.sleep(10); + + if (print) + System.out.print("StampedLockOptRead"); + new StampedLockOptimisticReadLoop().test(v, nthreads, iters); + Thread.sleep(10); + + if (print) + System.out.print("StampedLockReadWrite"); + new StampedLockReadWriteLoop().test(v, nthreads, iters); + Thread.sleep(10); + } } - static abstract class LockLoop implements Runnable { + abstract static class LockLoop implements Runnable { int v; int iters; volatile int result; @@ -150,7 +175,7 @@ public final class LockLoops { 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(); } @@ -176,7 +201,7 @@ public final class LockLoops { final int loop(int n) { int sum = 0; while (n-- > 0) { - synchronized(this) { + synchronized (this) { v = LoopHelpers.compute1(v); } sum += LoopHelpers.compute2(v); @@ -198,7 +223,7 @@ public final class LockLoops { } private static class NoLockVolatileLoop extends LockLoop { - volatile private int vv; + private volatile int vv; final int loop(int n) { int sum = 0; while (n-- > 0) { @@ -211,7 +236,7 @@ public final class LockLoops { } private static class ReentrantLockLoop extends LockLoop { - final private ReentrantLock lock = new ReentrantLock(); + private final ReentrantLock lock = new ReentrantLock(); final int loop(int n) { int sum = 0; while (n-- > 0) { @@ -229,7 +254,7 @@ public final class LockLoops { } 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) { int sum = 0; while (n-- > 0) { @@ -247,7 +272,25 @@ public final class LockLoops { } 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) { + int sum = 0; + while (n-- > 0) { + lock.lock(); + try { + v = LoopHelpers.compute1(v); + } + finally { + lock.unlock(); + } + sum += LoopHelpers.compute2(v); + } + return sum; + } + } + + private static class ReentrantReadLockLoop extends LockLoop { + private final Lock lock = new ReentrantReadWriteLock().readLock(); final int loop(int n) { int sum = 0; while (n-- > 0) { @@ -265,7 +308,7 @@ public final class LockLoops { } private static class ReentrantReadWriteLockLoop extends LockLoop { - final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final int loop(int n) { int sum = 0; while (n-- > 0) { @@ -309,7 +352,7 @@ public final class LockLoops { } 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) { int sum = 0; try { @@ -330,8 +373,9 @@ public final class LockLoops { return sum; } } + 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) { int sum = 0; try { @@ -354,7 +398,7 @@ public final class LockLoops { } 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) { int sum = 0; while (n-- > 0) { @@ -379,5 +423,80 @@ public final class LockLoops { } } + private static class StampedLockWriteLoop extends LockLoop { + private final StampedLock lock = new StampedLock(); + final int loop(int n) { + int sum = 0; + while (n-- > 0) { + long stamp = lock.writeLock(); + try { + v = LoopHelpers.compute1(v); + } + finally { + lock.unlockWrite(stamp); + } + sum += LoopHelpers.compute2(v); + } + return sum; + } + } + + private static class StampedLockReadLoop extends LockLoop { + private final StampedLock lock = new StampedLock(); + final int loop(int n) { + int sum = 0; + while (n-- > 0) { + long stamp = lock.readLock(); + try { + v = LoopHelpers.compute1(v); + } + finally { + lock.unlockRead(stamp); + } + sum += LoopHelpers.compute2(v); + } + return sum; + } + } + + private static class StampedLockOptimisticReadLoop extends LockLoop { + private final StampedLock lock = new StampedLock(); + final int loop(int n) { + int sum = 0; + while (n-- > 0) { + long stamp; + do { + stamp = lock.tryOptimisticRead(); + v = LoopHelpers.compute1(v); + } while (!lock.validate(stamp)); + sum += LoopHelpers.compute2(v); + } + return sum; + } + } + private static class StampedLockReadWriteLoop extends LockLoop { + private final StampedLock lock = new StampedLock(); + final int loop(int n) { + int sum = 0; + while (n-- > 0) { + int x; + long stamp = lock.readLock(); + try { + x = LoopHelpers.compute1(v); + } + finally { + lock.unlockRead(stamp); + } + stamp = lock.writeLock(); + try { + v = x; + } finally { + lock.unlockWrite(stamp); + } + sum += LoopHelpers.compute2(v); + } + return sum; + } + } }