--- jsr166/src/test/loops/LockLoops.java 2016/06/08 20:42:12 1.14 +++ jsr166/src/test/loops/LockLoops.java 2016/06/09 15:28:19 1.15 @@ -3,8 +3,9 @@ * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ + /* - * A simple test program. Feel free to play. + * Simple benchmark comparing various locking techniques. */ import java.util.*; @@ -91,6 +92,11 @@ 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); @@ -270,6 +276,24 @@ public final class LockLoops { 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) { lock.lock(); try { v = LoopHelpers.compute1(v);