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.12 by jsr166, Sat Dec 31 18:54:28 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   * @test
8   * @summary basic safety and liveness of ReentrantLocks, and other locks based on them
9   */
10  
11 < import java.util.concurrent.*;
12 < import java.util.concurrent.locks.*;
13 < import java.util.*;
11 > import java.util.concurrent.Callable;
12 > import java.util.concurrent.CyclicBarrier;
13 > import java.util.concurrent.ExecutorService;
14 > import java.util.concurrent.Executors;
15 > import java.util.concurrent.Semaphore;
16 > import java.util.concurrent.locks.Lock;
17 > import java.util.concurrent.locks.ReentrantLock;
18 > import java.util.concurrent.locks.ReentrantReadWriteLock;
19  
20   public final class CheckedLockLoops {
21      static final ExecutorService pool = Executors.newCachedThreadPool();
# Line 21 | Line 26 | public final class CheckedLockLoops {
26      public static void main(String[] args) throws Exception {
27          int maxThreads = 100;
28          int iters = 2000000;
29 <        if (args.length > 0)
29 >        if (args.length > 0)
30              maxThreads = Integer.parseInt(args[0]);
31          rng.setSeed(3122688L);
32          warmup(iters);
# Line 31 | Line 36 | public final class CheckedLockLoops {
36  
37      static void runTest(int maxThreads, int iters) throws Exception {
38          print = true;
39 <        int k = 1;
35 <        for (int i = 1; i <= maxThreads;) {
39 >        for (int k = 1, i = 1; i <= maxThreads;) {
40              System.out.println("Threads:" + i);
41              oneTest(i, iters / i);
42              if (i == k) {
43                  k = i << 1;
44                  i = i + (i >>> 1);
45 <            }
46 <            else
45 >            }
46 >            else
47                  i = k;
48          }
49 <    }    
49 >    }
50  
51      static void warmup(int iters) throws Exception {
52          print = false;
# Line 52 | Line 56 | public final class CheckedLockLoops {
56      }
57  
58      static void oneTest(int nthreads, int iters) throws Exception {
59 <        int fairIters = (nthreads <= 1)? iters : iters/20;
59 >        int fairIters = (nthreads <= 1) ? iters : iters/20;
60          int v = rng.next();
61  
62          if (print)
63              System.out.print("NoLock (1 thread)     ");
64          new NoLockLoop().test(v, 1, iters * nthreads);
65          Thread.sleep(10);
66 <
66 >
67          if (print)
68              System.out.print("ReentrantLock         ");
69          new ReentrantLockLoop().test(v, nthreads, iters);
# Line 91 | Line 95 | public final class CheckedLockLoops {
95              System.out.print("Semaphore             ");
96          new SemaphoreLoop().test(v, nthreads, iters);
97          Thread.sleep(10);
98 <        
98 >
99          if (print)
100              System.out.print("FairSemaphore         ");
101          new FairSemaphoreLoop().test(v, nthreads, fairIters);
# Line 106 | Line 110 | public final class CheckedLockLoops {
110              System.out.print("FairRWriteLock         ");
111          new FairReentrantWriteLockLoop().test(v, nthreads, fairIters);
112          Thread.sleep(10);
113 <        
113 >
114          if (print)
115              System.out.print("ReentrantReadWriteLock");
116          new ReentrantReadWriteLockLoop().test(v, nthreads, iters);
117          Thread.sleep(10);
118 <        
118 >
119          if (print)
120              System.out.print("FairRReadWriteLock     ");
121          new FairReentrantReadWriteLockLoop().test(v, nthreads, fairIters);
122          Thread.sleep(10);
123      }
124  
125 <    static abstract class LockLoop implements Runnable {
125 >    abstract static class LockLoop implements Runnable {
126          int value;
127          int checkValue;
128          int iters;
# Line 135 | Line 139 | public final class CheckedLockLoops {
139  
140          final int getValue() {
141              int v = value;
142 <            if (checkValue != ~(v ^ 0xAAAAAAAA))
142 >            if (checkValue != ~(v ^ 0xAAAAAAAA))
143                  ++failures;
144              return v;
145          }
# Line 144 | Line 148 | public final class CheckedLockLoops {
148              setValue(initialValue);
149              this.iters = iters;
150              barrier = new CyclicBarrier(nthreads+1, timer);
151 <            for (int i = 0; i < nthreads; ++i)
151 >            for (int i = 0; i < nthreads; ++i)
152                  pool.execute(this);
153              barrier.await();
154              barrier.await();
# Line 164 | Line 168 | public final class CheckedLockLoops {
168          abstract int loop(int n);
169          public final void run() {
170              try {
171 <                barrier.await();
171 >                barrier.await();
172                  result += loop(iters);
173                  barrier.await();
174              }
175 <            catch (Exception ie) {
176 <                return;
175 >            catch (Exception ie) {
176 >                return;
177              }
178          }
179  
# Line 197 | Line 201 | public final class CheckedLockLoops {
201              int sum = 0;
202              int x = 0;
203              while (n-- > 0) {
204 <                synchronized(this) {
204 >                synchronized (this) {
205                      x = setValue(LoopHelpers.compute1(getValue()));
206                  }
207                  sum += LoopHelpers.compute2(x);
# Line 207 | Line 211 | public final class CheckedLockLoops {
211      }
212  
213      private static class ReentrantLockLoop extends LockLoop {
214 <        final private ReentrantLock lock = new ReentrantLock();
214 >        private final ReentrantLock lock = new ReentrantLock();
215          final int loop(int n) {
216              final ReentrantLock lock = this.lock;
217              int sum = 0;
# Line 227 | Line 231 | public final class CheckedLockLoops {
231      }
232  
233      private static class MutexLoop extends LockLoop {
234 <        final private Mutex lock = new Mutex();
234 >        private final Mutex lock = new Mutex();
235          final int loop(int n) {
236              final Mutex lock = this.lock;
237              int sum = 0;
# Line 247 | Line 251 | public final class CheckedLockLoops {
251      }
252  
253      private static class LongMutexLoop extends LockLoop {
254 <        final private LongMutex lock = new LongMutex();
254 >        private final LongMutex lock = new LongMutex();
255          final int loop(int n) {
256              final LongMutex lock = this.lock;
257              int sum = 0;
# Line 267 | Line 271 | public final class CheckedLockLoops {
271      }
272  
273      private static class FairReentrantLockLoop extends LockLoop {
274 <        final private ReentrantLock lock = new ReentrantLock(true);
274 >        private final ReentrantLock lock = new ReentrantLock(true);
275          final int loop(int n) {
276              final ReentrantLock lock = this.lock;
277              int sum = 0;
# Line 287 | Line 291 | public final class CheckedLockLoops {
291      }
292  
293      private static class ReentrantWriteLockLoop extends LockLoop {
294 <        final private Lock lock = new ReentrantReadWriteLock().writeLock();
294 >        private final Lock lock = new ReentrantReadWriteLock().writeLock();
295          final int loop(int n) {
296              final Lock lock = this.lock;
297              int sum = 0;
# Line 327 | Line 331 | public final class CheckedLockLoops {
331      }
332  
333      private static class SemaphoreLoop extends LockLoop {
334 <        final private Semaphore sem = new Semaphore(1, false);
334 >        private final Semaphore sem = new Semaphore(1, false);
335          final int loop(int n) {
336              final Semaphore sem = this.sem;
337              int sum = 0;
# Line 346 | Line 350 | public final class CheckedLockLoops {
350          }
351      }
352      private static class FairSemaphoreLoop extends LockLoop {
353 <        final private Semaphore sem = new Semaphore(1, true);
353 >        private final Semaphore sem = new Semaphore(1, true);
354          final int loop(int n) {
355              final Semaphore sem = this.sem;
356              int sum = 0;
# Line 366 | Line 370 | public final class CheckedLockLoops {
370      }
371  
372      private static class ReentrantReadWriteLockLoop extends LockLoop {
373 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
373 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
374          final int loop(int n) {
375              final Lock rlock = lock.readLock();
376              final Lock wlock = lock.writeLock();
# Line 399 | Line 403 | public final class CheckedLockLoops {
403  
404      }
405  
402
406      private static class FairReentrantReadWriteLockLoop extends LockLoop {
407 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
407 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
408          final int loop(int n) {
409              final Lock rlock = lock.readLock();
410              final Lock wlock = lock.writeLock();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines