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.14 by jsr166, Wed Jun 8 20:42:12 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   * A simple test program. Feel free to play.
8   */
9  
10 + import java.util.*;
11   import java.util.concurrent.*;
12   import java.util.concurrent.locks.*;
12 import java.util.*;
13  
14   public final class LockLoops {
15      static final ExecutorService pool = Executors.newCachedThreadPool();
# Line 18 | Line 18 | public final class LockLoops {
18      static boolean doBuiltin = true;
19      static boolean doReadWrite = true;
20      static boolean doSemaphore = true;
21 <    static boolean doFair =  true;
21 >    static boolean doStampedLock = true;
22 >    static boolean doFair = true;
23  
24      public static void main(String[] args) throws Exception {
25          int maxThreads = 100;
# Line 93 | Line 94 | public final class LockLoops {
94                  System.out.print("ReentrantReadWriteLock");
95              new ReentrantReadWriteLockLoop().test(v, nthreads, iters);
96              Thread.sleep(10);
96
97          }
98  
99          if (doSemaphore) {
# Line 106 | Line 106 | public final class LockLoops {
106                  System.out.print("FairSemaphore         ");
107              new FairSemaphoreLoop().test(v, nthreads, iters);
108              Thread.sleep(10);
109
109          }
110  
111          if (doFair) {
# Line 126 | Line 125 | public final class LockLoops {
125                  new FairReentrantReadWriteLockLoop().test(v, nthreads, iters);
126                  Thread.sleep(10);
127              }
129
128          }
129  
130 +        if (doStampedLock) {
131 +            if (print)
132 +                System.out.print("StampedLockWrite   ");
133 +            new StampedLockWriteLoop().test(v, nthreads, iters);
134 +            Thread.sleep(10);
135 +
136 +            if (print)
137 +                System.out.print("StampedLockRead    ");
138 +            new StampedLockReadLoop().test(v, nthreads, iters);
139 +            Thread.sleep(10);
140 +
141 +            if (print)
142 +                System.out.print("StampedLockOptRead");
143 +            new StampedLockOptimisticReadLoop().test(v, nthreads, iters);
144 +            Thread.sleep(10);
145 +
146 +            if (print)
147 +                System.out.print("StampedLockReadWrite");
148 +            new StampedLockReadWriteLoop().test(v, nthreads, iters);
149 +            Thread.sleep(10);
150 +        }
151      }
152  
153 <    static abstract class LockLoop implements Runnable {
153 >    abstract static class LockLoop implements Runnable {
154          int v;
155          int iters;
156          volatile int result;
# Line 198 | Line 217 | public final class LockLoops {
217      }
218  
219      private static class NoLockVolatileLoop extends LockLoop {
220 <        volatile private int vv;
220 >        private volatile int vv;
221          final int loop(int n) {
222              int sum = 0;
223              while (n-- > 0) {
# Line 211 | Line 230 | public final class LockLoops {
230      }
231  
232      private static class ReentrantLockLoop extends LockLoop {
233 <        final private ReentrantLock lock = new ReentrantLock();
233 >        private final ReentrantLock lock = new ReentrantLock();
234          final int loop(int n) {
235              int sum = 0;
236              while (n-- > 0) {
# Line 229 | Line 248 | public final class LockLoops {
248      }
249  
250      private static class FairReentrantLockLoop extends LockLoop {
251 <        final private ReentrantLock lock = new ReentrantLock(true);
251 >        private final ReentrantLock lock = new ReentrantLock(true);
252          final int loop(int n) {
253              int sum = 0;
254              while (n-- > 0) {
# Line 247 | Line 266 | public final class LockLoops {
266      }
267  
268      private static class ReentrantWriteLockLoop extends LockLoop {
269 <        final private Lock lock = new ReentrantReadWriteLock().writeLock();
269 >        private final Lock lock = new ReentrantReadWriteLock().writeLock();
270          final int loop(int n) {
271              int sum = 0;
272              while (n-- > 0) {
# Line 265 | Line 284 | public final class LockLoops {
284      }
285  
286      private static class ReentrantReadWriteLockLoop extends LockLoop {
287 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
287 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
288          final int loop(int n) {
289              int sum = 0;
290              while (n-- > 0) {
# Line 309 | Line 328 | public final class LockLoops {
328      }
329  
330      private static class SemaphoreLoop extends LockLoop {
331 <        final private Semaphore sem = new Semaphore(1, false);
331 >        private final Semaphore sem = new Semaphore(1, false);
332          final int loop(int n) {
333              int sum = 0;
334              try {
# Line 330 | Line 349 | public final class LockLoops {
349              return sum;
350          }
351      }
352 +
353      private static class FairSemaphoreLoop extends LockLoop {
354 <        final private Semaphore sem = new Semaphore(1, true);
354 >        private final Semaphore sem = new Semaphore(1, true);
355          final int loop(int n) {
356              int sum = 0;
357              try {
# Line 354 | Line 374 | public final class LockLoops {
374      }
375  
376      private static class FairReentrantReadWriteLockLoop extends LockLoop {
377 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
377 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
378          final int loop(int n) {
379              int sum = 0;
380              while (n-- > 0) {
# Line 379 | Line 399 | public final class LockLoops {
399          }
400      }
401  
402 +    private static class StampedLockWriteLoop extends LockLoop {
403 +        private final StampedLock lock = new StampedLock();
404 +        final int loop(int n) {
405 +            int sum = 0;
406 +            while (n-- > 0) {
407 +                long stamp = lock.writeLock();
408 +                try {
409 +                    v = LoopHelpers.compute1(v);
410 +                }
411 +                finally {
412 +                    lock.unlockWrite(stamp);
413 +                }
414 +                sum += LoopHelpers.compute2(v);
415 +            }
416 +            return sum;
417 +        }
418 +    }
419 +
420 +    private static class StampedLockReadLoop extends LockLoop {
421 +        private final StampedLock lock = new StampedLock();
422 +        final int loop(int n) {
423 +            int sum = 0;
424 +            while (n-- > 0) {
425 +                long stamp = lock.readLock();
426 +                try {
427 +                    v = LoopHelpers.compute1(v);
428 +                }
429 +                finally {
430 +                    lock.unlockRead(stamp);
431 +                }
432 +                sum += LoopHelpers.compute2(v);
433 +            }
434 +            return sum;
435 +        }
436 +    }
437 +
438 +    private static class StampedLockOptimisticReadLoop extends LockLoop {
439 +        private final StampedLock lock = new StampedLock();
440 +        final int loop(int n) {
441 +            int sum = 0;
442 +            while (n-- > 0) {
443 +                long stamp;
444 +                do {
445 +                    stamp = lock.tryOptimisticRead();
446 +                    v = LoopHelpers.compute1(v);
447 +                } while (!lock.validate(stamp));
448 +                sum += LoopHelpers.compute2(v);
449 +            }
450 +            return sum;
451 +        }
452 +    }
453  
454 +    private static class StampedLockReadWriteLoop extends LockLoop {
455 +        private final StampedLock lock = new StampedLock();
456 +        final int loop(int n) {
457 +            int sum = 0;
458 +            while (n-- > 0) {
459 +                int x;
460 +                long stamp = lock.readLock();
461 +                try {
462 +                    x = LoopHelpers.compute1(v);
463 +                }
464 +                finally {
465 +                    lock.unlockRead(stamp);
466 +                }
467 +                stamp = lock.writeLock();
468 +                try {
469 +                    v = x;
470 +                } finally {
471 +                    lock.unlockWrite(stamp);
472 +                }
473 +                sum += LoopHelpers.compute2(v);
474 +            }
475 +            return sum;
476 +        }
477 +    }
478   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines