ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/LockLoops.java
(Generate patch)

Comparing jsr166/src/test/loops/LockLoops.java (file contents):
Revision 1.14 by jsr166, Wed Jun 8 20:42:12 2016 UTC vs.
Revision 1.16 by jsr166, Sat Dec 31 18:54:28 2016 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 +
7   /*
8 < * A simple test program. Feel free to play.
8 > * Simple benchmark comparing various locking techniques.
9   */
10  
11 < import java.util.*;
12 < import java.util.concurrent.*;
13 < import java.util.concurrent.locks.*;
11 > import java.util.concurrent.CyclicBarrier;
12 > import java.util.concurrent.ExecutorService;
13 > import java.util.concurrent.Executors;
14 > import java.util.concurrent.Semaphore;
15 > import java.util.concurrent.locks.Lock;
16 > import java.util.concurrent.locks.ReentrantLock;
17 > import java.util.concurrent.locks.ReentrantReadWriteLock;
18 > import java.util.concurrent.locks.StampedLock;
19  
20   public final class LockLoops {
21      static final ExecutorService pool = Executors.newCachedThreadPool();
# Line 91 | Line 97 | public final class LockLoops {
97              Thread.sleep(10);
98  
99              if (print)
100 +                System.out.print("ReentrantReadLock    ");
101 +            new ReentrantReadLockLoop().test(v, nthreads, iters);
102 +            Thread.sleep(10);
103 +
104 +            if (print)
105                  System.out.print("ReentrantReadWriteLock");
106              new ReentrantReadWriteLockLoop().test(v, nthreads, iters);
107              Thread.sleep(10);
# Line 270 | Line 281 | public final class LockLoops {
281          final int loop(int n) {
282              int sum = 0;
283              while (n-- > 0) {
284 +                lock.lock();
285 +                try {
286 +                    v = LoopHelpers.compute1(v);
287 +                }
288 +                finally {
289 +                    lock.unlock();
290 +                }
291 +                sum += LoopHelpers.compute2(v);
292 +            }
293 +            return sum;
294 +        }
295 +    }
296 +
297 +    private static class ReentrantReadLockLoop extends LockLoop {
298 +        private final Lock lock = new ReentrantReadWriteLock().readLock();
299 +        final int loop(int n) {
300 +            int sum = 0;
301 +            while (n-- > 0) {
302                  lock.lock();
303                  try {
304                      v = LoopHelpers.compute1(v);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines