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.13 by jsr166, Wed Jun 8 20:11:01 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/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.*;
7 import java.util.*;
13  
14   public final class LockLoops {
15      static final ExecutorService pool = Executors.newCachedThreadPool();
# Line 13 | 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;
26          int iters = 1000000;
27          int replications = 1;
28  
29 <        if (args.length > 0)
29 >        if (args.length > 0)
30              maxThreads = Integer.parseInt(args[0]);
31  
32 <        if (args.length > 1)
32 >        if (args.length > 1)
33              iters = Integer.parseInt(args[1]);
34  
35 <        if (args.length > 2)
35 >        if (args.length > 2)
36              replications = Integer.parseInt(args[2]);
37  
38          rng.setSeed(3122688L);
# Line 88 | Line 94 | public final class LockLoops {
94                  System.out.print("ReentrantReadWriteLock");
95              new ReentrantReadWriteLockLoop().test(v, nthreads, iters);
96              Thread.sleep(10);
91
97          }
98  
99          if (doSemaphore) {
# Line 101 | Line 106 | public final class LockLoops {
106                  System.out.print("FairSemaphore         ");
107              new FairSemaphoreLoop().test(v, nthreads, iters);
108              Thread.sleep(10);
104
109          }
110  
111          if (doFair) {
# Line 121 | Line 125 | public final class LockLoops {
125                  new FairReentrantReadWriteLockLoop().test(v, nthreads, iters);
126                  Thread.sleep(10);
127              }
124
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("StampedLockReadWrite");
143 +            new StampedLockReadLoop().test(v, nthreads, iters);
144 +            Thread.sleep(10);
145 +        }
146      }
147  
148 <    static abstract class LockLoop implements Runnable {
148 >    abstract static class LockLoop implements Runnable {
149          int v;
150          int iters;
151          volatile int result;
# Line 137 | Line 156 | public final class LockLoops {
156              v = initialValue;
157              this.iters = iters;
158              barrier = new CyclicBarrier(nthreads+1, timer);
159 <            for (int i = 0; i < nthreads; ++i)
159 >            for (int i = 0; i < nthreads; ++i)
160                  pool.execute(this);
161              barrier.await();
162              barrier.await();
# Line 145 | Line 164 | public final class LockLoops {
164              if (print) {
165                  long tpi = time / (iters * nthreads);
166                  System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update");
167 <                //                double secs = (double)(time) / 1000000000.0;
167 >                //                double secs = (double) time / 1000000000.0;
168                  //                System.out.print("\t " + secs + "s run time");
169                  System.out.println();
170              }
# Line 156 | Line 175 | public final class LockLoops {
175          abstract int loop(int n);
176          public final void run() {
177              try {
178 <                barrier.await();
178 >                barrier.await();
179                  result += loop(iters);
180                  barrier.await();
181              }
182 <            catch (Exception ie) {
183 <                return;
182 >            catch (Exception ie) {
183 >                return;
184              }
185          }
186  
# Line 171 | Line 190 | public final class LockLoops {
190          final int loop(int n) {
191              int sum = 0;
192              while (n-- > 0) {
193 <                synchronized(this) {
193 >                synchronized (this) {
194                      v = LoopHelpers.compute1(v);
195                  }
196                  sum += LoopHelpers.compute2(v);
# Line 193 | Line 212 | public final class LockLoops {
212      }
213  
214      private static class NoLockVolatileLoop extends LockLoop {
215 <        volatile private int vv;
215 >        private volatile int vv;
216          final int loop(int n) {
217              int sum = 0;
218              while (n-- > 0) {
# Line 206 | Line 225 | public final class LockLoops {
225      }
226  
227      private static class ReentrantLockLoop extends LockLoop {
228 <        final private ReentrantLock lock = new ReentrantLock();
228 >        private final ReentrantLock lock = new ReentrantLock();
229          final int loop(int n) {
230              int sum = 0;
231              while (n-- > 0) {
# Line 224 | Line 243 | public final class LockLoops {
243      }
244  
245      private static class FairReentrantLockLoop extends LockLoop {
246 <        final private ReentrantLock lock = new ReentrantLock(true);
246 >        private final ReentrantLock lock = new ReentrantLock(true);
247          final int loop(int n) {
248              int sum = 0;
249              while (n-- > 0) {
# Line 242 | Line 261 | public final class LockLoops {
261      }
262  
263      private static class ReentrantWriteLockLoop extends LockLoop {
264 <        final private Lock lock = new ReentrantReadWriteLock().writeLock();
264 >        private final Lock lock = new ReentrantReadWriteLock().writeLock();
265          final int loop(int n) {
266              int sum = 0;
267              while (n-- > 0) {
# Line 260 | Line 279 | public final class LockLoops {
279      }
280  
281      private static class ReentrantReadWriteLockLoop extends LockLoop {
282 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
282 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
283          final int loop(int n) {
284              int sum = 0;
285              while (n-- > 0) {
# Line 304 | Line 323 | public final class LockLoops {
323      }
324  
325      private static class SemaphoreLoop extends LockLoop {
326 <        final private Semaphore sem = new Semaphore(1, false);
326 >        private final Semaphore sem = new Semaphore(1, false);
327          final int loop(int n) {
328              int sum = 0;
329              try {
# Line 325 | Line 344 | public final class LockLoops {
344              return sum;
345          }
346      }
347 +
348      private static class FairSemaphoreLoop extends LockLoop {
349 <        final private Semaphore sem = new Semaphore(1, true);
349 >        private final Semaphore sem = new Semaphore(1, true);
350          final int loop(int n) {
351              int sum = 0;
352              try {
# Line 349 | Line 369 | public final class LockLoops {
369      }
370  
371      private static class FairReentrantReadWriteLockLoop extends LockLoop {
372 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
372 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
373          final int loop(int n) {
374              int sum = 0;
375              while (n-- > 0) {
# Line 374 | Line 394 | public final class LockLoops {
394          }
395      }
396  
397 +    private static class StampedLockWriteLoop extends LockLoop {
398 +        private final StampedLock lock = new StampedLock();
399 +        final int loop(int n) {
400 +            int sum = 0;
401 +            while (n-- > 0) {
402 +                long stamp = lock.writeLock();
403 +                try {
404 +                    v = LoopHelpers.compute1(v);
405 +                }
406 +                finally {
407 +                    lock.unlockWrite(stamp);
408 +                }
409 +                sum += LoopHelpers.compute2(v);
410 +            }
411 +            return sum;
412 +        }
413 +    }
414 +
415 +    private static class StampedLockReadLoop extends LockLoop {
416 +        private final StampedLock lock = new StampedLock();
417 +        final int loop(int n) {
418 +            int sum = 0;
419 +            while (n-- > 0) {
420 +                long stamp = lock.readLock();
421 +                try {
422 +                    v = LoopHelpers.compute1(v);
423 +                }
424 +                finally {
425 +                    lock.unlockRead(stamp);
426 +                }
427 +                sum += LoopHelpers.compute2(v);
428 +            }
429 +            return sum;
430 +        }
431 +    }
432  
433 +    private static class StampedLockReadWriteLoop extends LockLoop {
434 +        private final StampedLock lock = new StampedLock();
435 +        final int loop(int n) {
436 +            int sum = 0;
437 +            while (n-- > 0) {
438 +                int x;
439 +                long stamp = lock.readLock();
440 +                try {
441 +                    x = LoopHelpers.compute1(v);
442 +                }
443 +                finally {
444 +                    lock.unlockRead(stamp);
445 +                }
446 +                stamp = lock.writeLock();
447 +                try {
448 +                    v = x;
449 +                } finally {
450 +                    lock.unlockWrite(stamp);
451 +                }
452 +                sum += LoopHelpers.compute2(v);
453 +            }
454 +            return sum;
455 +        }
456 +    }
457   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines