ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CheckedLockLoops.java
(Generate patch)

Comparing jsr166/src/test/loops/CheckedLockLoops.java (file contents):
Revision 1.3 by jsr166, Sat Aug 1 22:12:59 2009 UTC vs.
Revision 1.10 by jsr166, Wed Dec 31 17:00:58 2014 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   * @test
8   * @summary basic safety and liveness of ReentrantLocks, and other locks based on them
9   */
10  
11 + import java.util.*;
12   import java.util.concurrent.*;
13   import java.util.concurrent.locks.*;
13 import java.util.*;
14  
15   public final class CheckedLockLoops {
16      static final ExecutorService pool = Executors.newCachedThreadPool();
# Line 21 | Line 21 | public final class CheckedLockLoops {
21      public static void main(String[] args) throws Exception {
22          int maxThreads = 100;
23          int iters = 2000000;
24 <        if (args.length > 0)
24 >        if (args.length > 0)
25              maxThreads = Integer.parseInt(args[0]);
26          rng.setSeed(3122688L);
27          warmup(iters);
# Line 31 | Line 31 | public final class CheckedLockLoops {
31  
32      static void runTest(int maxThreads, int iters) throws Exception {
33          print = true;
34 <        int k = 1;
35 <        for (int i = 1; i <= maxThreads;) {
34 >        for (int k = 1, i = 1; i <= maxThreads;) {
35              System.out.println("Threads:" + i);
36              oneTest(i, iters / i);
37              if (i == k) {
38                  k = i << 1;
39                  i = i + (i >>> 1);
40 <            }
41 <            else
40 >            }
41 >            else
42                  i = k;
43          }
44 <    }    
44 >    }
45  
46      static void warmup(int iters) throws Exception {
47          print = false;
# Line 52 | Line 51 | public final class CheckedLockLoops {
51      }
52  
53      static void oneTest(int nthreads, int iters) throws Exception {
54 <        int fairIters = (nthreads <= 1)? iters : iters/20;
54 >        int fairIters = (nthreads <= 1) ? iters : iters/20;
55          int v = rng.next();
56  
57          if (print)
58              System.out.print("NoLock (1 thread)     ");
59          new NoLockLoop().test(v, 1, iters * nthreads);
60          Thread.sleep(10);
61 <
61 >
62          if (print)
63              System.out.print("ReentrantLock         ");
64          new ReentrantLockLoop().test(v, nthreads, iters);
# Line 91 | Line 90 | public final class CheckedLockLoops {
90              System.out.print("Semaphore             ");
91          new SemaphoreLoop().test(v, nthreads, iters);
92          Thread.sleep(10);
93 <        
93 >
94          if (print)
95              System.out.print("FairSemaphore         ");
96          new FairSemaphoreLoop().test(v, nthreads, fairIters);
# Line 106 | Line 105 | public final class CheckedLockLoops {
105              System.out.print("FairRWriteLock         ");
106          new FairReentrantWriteLockLoop().test(v, nthreads, fairIters);
107          Thread.sleep(10);
108 <        
108 >
109          if (print)
110              System.out.print("ReentrantReadWriteLock");
111          new ReentrantReadWriteLockLoop().test(v, nthreads, iters);
112          Thread.sleep(10);
113 <        
113 >
114          if (print)
115              System.out.print("FairRReadWriteLock     ");
116          new FairReentrantReadWriteLockLoop().test(v, nthreads, fairIters);
117          Thread.sleep(10);
118      }
119  
120 <    static abstract class LockLoop implements Runnable {
120 >    abstract static class LockLoop implements Runnable {
121          int value;
122          int checkValue;
123          int iters;
# Line 135 | Line 134 | public final class CheckedLockLoops {
134  
135          final int getValue() {
136              int v = value;
137 <            if (checkValue != ~(v ^ 0xAAAAAAAA))
137 >            if (checkValue != ~(v ^ 0xAAAAAAAA))
138                  ++failures;
139              return v;
140          }
# Line 144 | Line 143 | public final class CheckedLockLoops {
143              setValue(initialValue);
144              this.iters = iters;
145              barrier = new CyclicBarrier(nthreads+1, timer);
146 <            for (int i = 0; i < nthreads; ++i)
146 >            for (int i = 0; i < nthreads; ++i)
147                  pool.execute(this);
148              barrier.await();
149              barrier.await();
# Line 164 | Line 163 | public final class CheckedLockLoops {
163          abstract int loop(int n);
164          public final void run() {
165              try {
166 <                barrier.await();
166 >                barrier.await();
167                  result += loop(iters);
168                  barrier.await();
169              }
170 <            catch (Exception ie) {
171 <                return;
170 >            catch (Exception ie) {
171 >                return;
172              }
173          }
174  
# Line 197 | Line 196 | public final class CheckedLockLoops {
196              int sum = 0;
197              int x = 0;
198              while (n-- > 0) {
199 <                synchronized(this) {
199 >                synchronized (this) {
200                      x = setValue(LoopHelpers.compute1(getValue()));
201                  }
202                  sum += LoopHelpers.compute2(x);
# Line 207 | Line 206 | public final class CheckedLockLoops {
206      }
207  
208      private static class ReentrantLockLoop extends LockLoop {
209 <        final private ReentrantLock lock = new ReentrantLock();
209 >        private final ReentrantLock lock = new ReentrantLock();
210          final int loop(int n) {
211              final ReentrantLock lock = this.lock;
212              int sum = 0;
# Line 227 | Line 226 | public final class CheckedLockLoops {
226      }
227  
228      private static class MutexLoop extends LockLoop {
229 <        final private Mutex lock = new Mutex();
229 >        private final Mutex lock = new Mutex();
230          final int loop(int n) {
231              final Mutex lock = this.lock;
232              int sum = 0;
# Line 247 | Line 246 | public final class CheckedLockLoops {
246      }
247  
248      private static class LongMutexLoop extends LockLoop {
249 <        final private LongMutex lock = new LongMutex();
249 >        private final LongMutex lock = new LongMutex();
250          final int loop(int n) {
251              final LongMutex lock = this.lock;
252              int sum = 0;
# Line 267 | Line 266 | public final class CheckedLockLoops {
266      }
267  
268      private static class FairReentrantLockLoop extends LockLoop {
269 <        final private ReentrantLock lock = new ReentrantLock(true);
269 >        private final ReentrantLock lock = new ReentrantLock(true);
270          final int loop(int n) {
271              final ReentrantLock lock = this.lock;
272              int sum = 0;
# Line 287 | Line 286 | public final class CheckedLockLoops {
286      }
287  
288      private static class ReentrantWriteLockLoop extends LockLoop {
289 <        final private Lock lock = new ReentrantReadWriteLock().writeLock();
289 >        private final Lock lock = new ReentrantReadWriteLock().writeLock();
290          final int loop(int n) {
291              final Lock lock = this.lock;
292              int sum = 0;
# Line 327 | Line 326 | public final class CheckedLockLoops {
326      }
327  
328      private static class SemaphoreLoop extends LockLoop {
329 <        final private Semaphore sem = new Semaphore(1, false);
329 >        private final Semaphore sem = new Semaphore(1, false);
330          final int loop(int n) {
331              final Semaphore sem = this.sem;
332              int sum = 0;
# Line 346 | Line 345 | public final class CheckedLockLoops {
345          }
346      }
347      private static class FairSemaphoreLoop extends LockLoop {
348 <        final private Semaphore sem = new Semaphore(1, true);
348 >        private final Semaphore sem = new Semaphore(1, true);
349          final int loop(int n) {
350              final Semaphore sem = this.sem;
351              int sum = 0;
# Line 366 | Line 365 | public final class CheckedLockLoops {
365      }
366  
367      private static class ReentrantReadWriteLockLoop extends LockLoop {
368 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
368 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
369          final int loop(int n) {
370              final Lock rlock = lock.readLock();
371              final Lock wlock = lock.writeLock();
# Line 401 | Line 400 | public final class CheckedLockLoops {
400  
401  
402      private static class FairReentrantReadWriteLockLoop extends LockLoop {
403 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
403 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
404          final int loop(int n) {
405              final Lock rlock = lock.readLock();
406              final Lock wlock = lock.writeLock();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines