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.1 by dl, Mon May 2 19:19:38 2005 UTC vs.
Revision 1.16 by jsr166, Sat Dec 31 18:54:28 2016 UTC

# Line 1 | Line 1
1   /*
2 < * A simple test program. Feel free to play.
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/publicdomain/zero/1.0/
5   */
6  
7 < import java.util.concurrent.*;
8 < import java.util.concurrent.locks.*;
9 < import java.util.*;
7 > /*
8 > * Simple benchmark comparing various locking techniques.
9 > */
10 >
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 13 | Line 24 | public final class LockLoops {
24      static boolean doBuiltin = true;
25      static boolean doReadWrite = true;
26      static boolean doSemaphore = true;
27 <    static boolean doFair =  true;
27 >    static boolean doStampedLock = true;
28 >    static boolean doFair = true;
29  
30      public static void main(String[] args) throws Exception {
31          int maxThreads = 100;
32          int iters = 1000000;
33          int replications = 1;
34  
35 <        if (args.length > 0)
35 >        if (args.length > 0)
36              maxThreads = Integer.parseInt(args[0]);
37  
38 <        if (args.length > 1)
38 >        if (args.length > 1)
39              iters = Integer.parseInt(args[1]);
40  
41 <        if (args.length > 2)
41 >        if (args.length > 2)
42              replications = Integer.parseInt(args[2]);
43  
44          rng.setSeed(3122688L);
# Line 85 | 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);
91
108          }
109  
110          if (doSemaphore) {
# Line 101 | Line 117 | public final class LockLoops {
117                  System.out.print("FairSemaphore         ");
118              new FairSemaphoreLoop().test(v, nthreads, iters);
119              Thread.sleep(10);
104
120          }
121  
122          if (doFair) {
# Line 121 | Line 136 | public final class LockLoops {
136                  new FairReentrantReadWriteLockLoop().test(v, nthreads, iters);
137                  Thread.sleep(10);
138              }
124
139          }
140  
141 +        if (doStampedLock) {
142 +            if (print)
143 +                System.out.print("StampedLockWrite   ");
144 +            new StampedLockWriteLoop().test(v, nthreads, iters);
145 +            Thread.sleep(10);
146 +
147 +            if (print)
148 +                System.out.print("StampedLockRead    ");
149 +            new StampedLockReadLoop().test(v, nthreads, iters);
150 +            Thread.sleep(10);
151 +
152 +            if (print)
153 +                System.out.print("StampedLockOptRead");
154 +            new StampedLockOptimisticReadLoop().test(v, nthreads, iters);
155 +            Thread.sleep(10);
156 +
157 +            if (print)
158 +                System.out.print("StampedLockReadWrite");
159 +            new StampedLockReadWriteLoop().test(v, nthreads, iters);
160 +            Thread.sleep(10);
161 +        }
162      }
163  
164 <    static abstract class LockLoop implements Runnable {
164 >    abstract static class LockLoop implements Runnable {
165          int v;
166          int iters;
167          volatile int result;
# Line 137 | Line 172 | public final class LockLoops {
172              v = initialValue;
173              this.iters = iters;
174              barrier = new CyclicBarrier(nthreads+1, timer);
175 <            for (int i = 0; i < nthreads; ++i)
175 >            for (int i = 0; i < nthreads; ++i)
176                  pool.execute(this);
177              barrier.await();
178              barrier.await();
# Line 145 | Line 180 | public final class LockLoops {
180              if (print) {
181                  long tpi = time / (iters * nthreads);
182                  System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update");
183 <                //                double secs = (double)(time) / 1000000000.0;
183 >                //                double secs = (double) time / 1000000000.0;
184                  //                System.out.print("\t " + secs + "s run time");
185                  System.out.println();
186              }
# Line 156 | Line 191 | public final class LockLoops {
191          abstract int loop(int n);
192          public final void run() {
193              try {
194 <                barrier.await();
194 >                barrier.await();
195                  result += loop(iters);
196                  barrier.await();
197              }
198 <            catch (Exception ie) {
199 <                return;
198 >            catch (Exception ie) {
199 >                return;
200              }
201          }
202  
# Line 171 | Line 206 | public final class LockLoops {
206          final int loop(int n) {
207              int sum = 0;
208              while (n-- > 0) {
209 <                synchronized(this) {
209 >                synchronized (this) {
210                      v = LoopHelpers.compute1(v);
211                  }
212                  sum += LoopHelpers.compute2(v);
# Line 193 | Line 228 | public final class LockLoops {
228      }
229  
230      private static class NoLockVolatileLoop extends LockLoop {
231 <        volatile private int vv;
231 >        private volatile int vv;
232          final int loop(int n) {
233              int sum = 0;
234              while (n-- > 0) {
# Line 206 | Line 241 | public final class LockLoops {
241      }
242  
243      private static class ReentrantLockLoop extends LockLoop {
244 <        final private ReentrantLock lock = new ReentrantLock();
244 >        private final ReentrantLock lock = new ReentrantLock();
245          final int loop(int n) {
246              int sum = 0;
247              while (n-- > 0) {
# Line 224 | Line 259 | public final class LockLoops {
259      }
260  
261      private static class FairReentrantLockLoop extends LockLoop {
262 <        final private ReentrantLock lock = new ReentrantLock(true);
262 >        private final ReentrantLock lock = new ReentrantLock(true);
263          final int loop(int n) {
264              int sum = 0;
265              while (n-- > 0) {
# Line 242 | Line 277 | public final class LockLoops {
277      }
278  
279      private static class ReentrantWriteLockLoop extends LockLoop {
280 <        final private Lock lock = new ReentrantReadWriteLock().writeLock();
280 >        private final Lock lock = new ReentrantReadWriteLock().writeLock();
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) {
# Line 260 | Line 313 | public final class LockLoops {
313      }
314  
315      private static class ReentrantReadWriteLockLoop extends LockLoop {
316 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
316 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
317          final int loop(int n) {
318              int sum = 0;
319              while (n-- > 0) {
# Line 304 | Line 357 | public final class LockLoops {
357      }
358  
359      private static class SemaphoreLoop extends LockLoop {
360 <        final private Semaphore sem = new Semaphore(1, false);
360 >        private final Semaphore sem = new Semaphore(1, false);
361          final int loop(int n) {
362              int sum = 0;
363              try {
# Line 325 | Line 378 | public final class LockLoops {
378              return sum;
379          }
380      }
381 +
382      private static class FairSemaphoreLoop extends LockLoop {
383 <        final private Semaphore sem = new Semaphore(1, true);
383 >        private final Semaphore sem = new Semaphore(1, true);
384          final int loop(int n) {
385              int sum = 0;
386              try {
# Line 349 | Line 403 | public final class LockLoops {
403      }
404  
405      private static class FairReentrantReadWriteLockLoop extends LockLoop {
406 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
406 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
407          final int loop(int n) {
408              int sum = 0;
409              while (n-- > 0) {
# Line 374 | Line 428 | public final class LockLoops {
428          }
429      }
430  
431 +    private static class StampedLockWriteLoop extends LockLoop {
432 +        private final StampedLock lock = new StampedLock();
433 +        final int loop(int n) {
434 +            int sum = 0;
435 +            while (n-- > 0) {
436 +                long stamp = lock.writeLock();
437 +                try {
438 +                    v = LoopHelpers.compute1(v);
439 +                }
440 +                finally {
441 +                    lock.unlockWrite(stamp);
442 +                }
443 +                sum += LoopHelpers.compute2(v);
444 +            }
445 +            return sum;
446 +        }
447 +    }
448 +
449 +    private static class StampedLockReadLoop extends LockLoop {
450 +        private final StampedLock lock = new StampedLock();
451 +        final int loop(int n) {
452 +            int sum = 0;
453 +            while (n-- > 0) {
454 +                long stamp = lock.readLock();
455 +                try {
456 +                    v = LoopHelpers.compute1(v);
457 +                }
458 +                finally {
459 +                    lock.unlockRead(stamp);
460 +                }
461 +                sum += LoopHelpers.compute2(v);
462 +            }
463 +            return sum;
464 +        }
465 +    }
466 +
467 +    private static class StampedLockOptimisticReadLoop extends LockLoop {
468 +        private final StampedLock lock = new StampedLock();
469 +        final int loop(int n) {
470 +            int sum = 0;
471 +            while (n-- > 0) {
472 +                long stamp;
473 +                do {
474 +                    stamp = lock.tryOptimisticRead();
475 +                    v = LoopHelpers.compute1(v);
476 +                } while (!lock.validate(stamp));
477 +                sum += LoopHelpers.compute2(v);
478 +            }
479 +            return sum;
480 +        }
481 +    }
482  
483 +    private static class StampedLockReadWriteLoop extends LockLoop {
484 +        private final StampedLock lock = new StampedLock();
485 +        final int loop(int n) {
486 +            int sum = 0;
487 +            while (n-- > 0) {
488 +                int x;
489 +                long stamp = lock.readLock();
490 +                try {
491 +                    x = LoopHelpers.compute1(v);
492 +                }
493 +                finally {
494 +                    lock.unlockRead(stamp);
495 +                }
496 +                stamp = lock.writeLock();
497 +                try {
498 +                    v = x;
499 +                } finally {
500 +                    lock.unlockWrite(stamp);
501 +                }
502 +                sum += LoopHelpers.compute2(v);
503 +            }
504 +            return sum;
505 +        }
506 +    }
507   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines