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.12 by jsr166, Wed Jun 8 20:01:45 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      }
142  
143 <    static abstract class LockLoop implements Runnable {
143 >    abstract static class LockLoop implements Runnable {
144          int v;
145          int iters;
146          volatile int result;
# Line 198 | Line 207 | public final class LockLoops {
207      }
208  
209      private static class NoLockVolatileLoop extends LockLoop {
210 <        volatile private int vv;
210 >        private volatile int vv;
211          final int loop(int n) {
212              int sum = 0;
213              while (n-- > 0) {
# Line 211 | Line 220 | public final class LockLoops {
220      }
221  
222      private static class ReentrantLockLoop extends LockLoop {
223 <        final private ReentrantLock lock = new ReentrantLock();
223 >        private final ReentrantLock lock = new ReentrantLock();
224          final int loop(int n) {
225              int sum = 0;
226              while (n-- > 0) {
# Line 229 | Line 238 | public final class LockLoops {
238      }
239  
240      private static class FairReentrantLockLoop extends LockLoop {
241 <        final private ReentrantLock lock = new ReentrantLock(true);
241 >        private final ReentrantLock lock = new ReentrantLock(true);
242          final int loop(int n) {
243              int sum = 0;
244              while (n-- > 0) {
# Line 247 | Line 256 | public final class LockLoops {
256      }
257  
258      private static class ReentrantWriteLockLoop extends LockLoop {
259 <        final private Lock lock = new ReentrantReadWriteLock().writeLock();
259 >        private final Lock lock = new ReentrantReadWriteLock().writeLock();
260          final int loop(int n) {
261              int sum = 0;
262              while (n-- > 0) {
# Line 265 | Line 274 | public final class LockLoops {
274      }
275  
276      private static class ReentrantReadWriteLockLoop extends LockLoop {
277 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
277 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
278          final int loop(int n) {
279              int sum = 0;
280              while (n-- > 0) {
# Line 309 | Line 318 | public final class LockLoops {
318      }
319  
320      private static class SemaphoreLoop extends LockLoop {
321 <        final private Semaphore sem = new Semaphore(1, false);
321 >        private final Semaphore sem = new Semaphore(1, false);
322          final int loop(int n) {
323              int sum = 0;
324              try {
# Line 330 | Line 339 | public final class LockLoops {
339              return sum;
340          }
341      }
342 +
343      private static class FairSemaphoreLoop extends LockLoop {
344 <        final private Semaphore sem = new Semaphore(1, true);
344 >        private final Semaphore sem = new Semaphore(1, true);
345          final int loop(int n) {
346              int sum = 0;
347              try {
# Line 354 | Line 364 | public final class LockLoops {
364      }
365  
366      private static class FairReentrantReadWriteLockLoop extends LockLoop {
367 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
367 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
368          final int loop(int n) {
369              int sum = 0;
370              while (n-- > 0) {
# Line 379 | Line 389 | public final class LockLoops {
389          }
390      }
391  
392 +    private static class StampedLockWriteLoop extends LockLoop {
393 +        private final StampedLock lock = new StampedLock();
394 +        final int loop(int n) {
395 +            int sum = 0;
396 +            while (n-- > 0) {
397 +                long stamp = lock.writeLock();
398 +                try {
399 +                    v = LoopHelpers.compute1(v);
400 +                }
401 +                finally {
402 +                    lock.unlockWrite(stamp);
403 +                }
404 +                sum += LoopHelpers.compute2(v);
405 +            }
406 +            return sum;
407 +        }
408 +    }
409  
410 +    private static class StampedLockReadLoop extends LockLoop {
411 +        private final StampedLock lock = new StampedLock();
412 +        final int loop(int n) {
413 +            int sum = 0;
414 +            while (n-- > 0) {
415 +                long stamp = lock.readLock();
416 +                try {
417 +                    v = LoopHelpers.compute1(v);
418 +                }
419 +                finally {
420 +                    lock.unlockRead(stamp);
421 +                }
422 +                sum += LoopHelpers.compute2(v);
423 +            }
424 +            return sum;
425 +        }
426 +    }
427   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines