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.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/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      }
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 137 | Line 151 | public final class LockLoops {
151              v = initialValue;
152              this.iters = iters;
153              barrier = new CyclicBarrier(nthreads+1, timer);
154 <            for (int i = 0; i < nthreads; ++i)
154 >            for (int i = 0; i < nthreads; ++i)
155                  pool.execute(this);
156              barrier.await();
157              barrier.await();
# Line 145 | Line 159 | public final class LockLoops {
159              if (print) {
160                  long tpi = time / (iters * nthreads);
161                  System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update");
162 <                //                double secs = (double)(time) / 1000000000.0;
162 >                //                double secs = (double) time / 1000000000.0;
163                  //                System.out.print("\t " + secs + "s run time");
164                  System.out.println();
165              }
# Line 156 | Line 170 | public final class LockLoops {
170          abstract int loop(int n);
171          public final void run() {
172              try {
173 <                barrier.await();
173 >                barrier.await();
174                  result += loop(iters);
175                  barrier.await();
176              }
177 <            catch (Exception ie) {
178 <                return;
177 >            catch (Exception ie) {
178 >                return;
179              }
180          }
181  
# Line 171 | Line 185 | public final class LockLoops {
185          final int loop(int n) {
186              int sum = 0;
187              while (n-- > 0) {
188 <                synchronized(this) {
188 >                synchronized (this) {
189                      v = LoopHelpers.compute1(v);
190                  }
191                  sum += LoopHelpers.compute2(v);
# Line 193 | 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 206 | 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 224 | 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 242 | 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 260 | 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 304 | 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 325 | 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 349 | 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 374 | 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