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.5 by jsr166, Thu Sep 16 03:57:13 2010 UTC vs.
Revision 1.15 by jsr166, Thu Jun 9 15:28:19 2016 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
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.*;
12 import java.util.*;
14  
15   public final class LockLoops {
16      static final ExecutorService pool = Executors.newCachedThreadPool();
# Line 18 | Line 19 | public final class LockLoops {
19      static boolean doBuiltin = true;
20      static boolean doReadWrite = true;
21      static boolean doSemaphore = true;
22 <    static boolean doFair =  true;
22 >    static boolean doStampedLock = true;
23 >    static boolean doFair = true;
24  
25      public static void main(String[] args) throws Exception {
26          int maxThreads = 100;
# Line 90 | Line 92 | public final class LockLoops {
92              Thread.sleep(10);
93  
94              if (print)
95 +                System.out.print("ReentrantReadLock    ");
96 +            new ReentrantReadLockLoop().test(v, nthreads, iters);
97 +            Thread.sleep(10);
98 +
99 +            if (print)
100                  System.out.print("ReentrantReadWriteLock");
101              new ReentrantReadWriteLockLoop().test(v, nthreads, iters);
102              Thread.sleep(10);
96
103          }
104  
105          if (doSemaphore) {
# Line 106 | Line 112 | public final class LockLoops {
112                  System.out.print("FairSemaphore         ");
113              new FairSemaphoreLoop().test(v, nthreads, iters);
114              Thread.sleep(10);
109
115          }
116  
117          if (doFair) {
# Line 126 | Line 131 | public final class LockLoops {
131                  new FairReentrantReadWriteLockLoop().test(v, nthreads, iters);
132                  Thread.sleep(10);
133              }
129
134          }
135  
136 +        if (doStampedLock) {
137 +            if (print)
138 +                System.out.print("StampedLockWrite   ");
139 +            new StampedLockWriteLoop().test(v, nthreads, iters);
140 +            Thread.sleep(10);
141 +
142 +            if (print)
143 +                System.out.print("StampedLockRead    ");
144 +            new StampedLockReadLoop().test(v, nthreads, iters);
145 +            Thread.sleep(10);
146 +
147 +            if (print)
148 +                System.out.print("StampedLockOptRead");
149 +            new StampedLockOptimisticReadLoop().test(v, nthreads, iters);
150 +            Thread.sleep(10);
151 +
152 +            if (print)
153 +                System.out.print("StampedLockReadWrite");
154 +            new StampedLockReadWriteLoop().test(v, nthreads, iters);
155 +            Thread.sleep(10);
156 +        }
157      }
158  
159 <    static abstract class LockLoop implements Runnable {
159 >    abstract static class LockLoop implements Runnable {
160          int v;
161          int iters;
162          volatile int result;
# Line 198 | Line 223 | public final class LockLoops {
223      }
224  
225      private static class NoLockVolatileLoop extends LockLoop {
226 <        volatile private int vv;
226 >        private volatile int vv;
227          final int loop(int n) {
228              int sum = 0;
229              while (n-- > 0) {
# Line 211 | Line 236 | public final class LockLoops {
236      }
237  
238      private static class ReentrantLockLoop extends LockLoop {
239 <        final private ReentrantLock lock = new ReentrantLock();
239 >        private final ReentrantLock lock = new ReentrantLock();
240          final int loop(int n) {
241              int sum = 0;
242              while (n-- > 0) {
# Line 229 | Line 254 | public final class LockLoops {
254      }
255  
256      private static class FairReentrantLockLoop extends LockLoop {
257 <        final private ReentrantLock lock = new ReentrantLock(true);
257 >        private final ReentrantLock lock = new ReentrantLock(true);
258          final int loop(int n) {
259              int sum = 0;
260              while (n-- > 0) {
# Line 247 | Line 272 | public final class LockLoops {
272      }
273  
274      private static class ReentrantWriteLockLoop extends LockLoop {
275 <        final private Lock lock = new ReentrantReadWriteLock().writeLock();
275 >        private final Lock lock = new ReentrantReadWriteLock().writeLock();
276 >        final int loop(int n) {
277 >            int sum = 0;
278 >            while (n-- > 0) {
279 >                lock.lock();
280 >                try {
281 >                    v = LoopHelpers.compute1(v);
282 >                }
283 >                finally {
284 >                    lock.unlock();
285 >                }
286 >                sum += LoopHelpers.compute2(v);
287 >            }
288 >            return sum;
289 >        }
290 >    }
291 >
292 >    private static class ReentrantReadLockLoop extends LockLoop {
293 >        private final Lock lock = new ReentrantReadWriteLock().readLock();
294          final int loop(int n) {
295              int sum = 0;
296              while (n-- > 0) {
# Line 265 | Line 308 | public final class LockLoops {
308      }
309  
310      private static class ReentrantReadWriteLockLoop extends LockLoop {
311 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
311 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
312          final int loop(int n) {
313              int sum = 0;
314              while (n-- > 0) {
# Line 309 | Line 352 | public final class LockLoops {
352      }
353  
354      private static class SemaphoreLoop extends LockLoop {
355 <        final private Semaphore sem = new Semaphore(1, false);
355 >        private final Semaphore sem = new Semaphore(1, false);
356          final int loop(int n) {
357              int sum = 0;
358              try {
# Line 330 | Line 373 | public final class LockLoops {
373              return sum;
374          }
375      }
376 +
377      private static class FairSemaphoreLoop extends LockLoop {
378 <        final private Semaphore sem = new Semaphore(1, true);
378 >        private final Semaphore sem = new Semaphore(1, true);
379          final int loop(int n) {
380              int sum = 0;
381              try {
# Line 354 | Line 398 | public final class LockLoops {
398      }
399  
400      private static class FairReentrantReadWriteLockLoop extends LockLoop {
401 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
401 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
402          final int loop(int n) {
403              int sum = 0;
404              while (n-- > 0) {
# Line 379 | Line 423 | public final class LockLoops {
423          }
424      }
425  
426 +    private static class StampedLockWriteLoop extends LockLoop {
427 +        private final StampedLock lock = new StampedLock();
428 +        final int loop(int n) {
429 +            int sum = 0;
430 +            while (n-- > 0) {
431 +                long stamp = lock.writeLock();
432 +                try {
433 +                    v = LoopHelpers.compute1(v);
434 +                }
435 +                finally {
436 +                    lock.unlockWrite(stamp);
437 +                }
438 +                sum += LoopHelpers.compute2(v);
439 +            }
440 +            return sum;
441 +        }
442 +    }
443 +
444 +    private static class StampedLockReadLoop extends LockLoop {
445 +        private final StampedLock lock = new StampedLock();
446 +        final int loop(int n) {
447 +            int sum = 0;
448 +            while (n-- > 0) {
449 +                long stamp = lock.readLock();
450 +                try {
451 +                    v = LoopHelpers.compute1(v);
452 +                }
453 +                finally {
454 +                    lock.unlockRead(stamp);
455 +                }
456 +                sum += LoopHelpers.compute2(v);
457 +            }
458 +            return sum;
459 +        }
460 +    }
461 +
462 +    private static class StampedLockOptimisticReadLoop extends LockLoop {
463 +        private final StampedLock lock = new StampedLock();
464 +        final int loop(int n) {
465 +            int sum = 0;
466 +            while (n-- > 0) {
467 +                long stamp;
468 +                do {
469 +                    stamp = lock.tryOptimisticRead();
470 +                    v = LoopHelpers.compute1(v);
471 +                } while (!lock.validate(stamp));
472 +                sum += LoopHelpers.compute2(v);
473 +            }
474 +            return sum;
475 +        }
476 +    }
477  
478 +    private static class StampedLockReadWriteLoop extends LockLoop {
479 +        private final StampedLock lock = new StampedLock();
480 +        final int loop(int n) {
481 +            int sum = 0;
482 +            while (n-- > 0) {
483 +                int x;
484 +                long stamp = lock.readLock();
485 +                try {
486 +                    x = LoopHelpers.compute1(v);
487 +                }
488 +                finally {
489 +                    lock.unlockRead(stamp);
490 +                }
491 +                stamp = lock.writeLock();
492 +                try {
493 +                    v = x;
494 +                } finally {
495 +                    lock.unlockWrite(stamp);
496 +                }
497 +                sum += LoopHelpers.compute2(v);
498 +            }
499 +            return sum;
500 +        }
501 +    }
502   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines