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.2 by dl, Mon May 9 19:33:30 2005 UTC vs.
Revision 1.13 by jsr166, Sat Dec 31 22:17:40 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.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  
19   public final class CheckedLockLoops {
20      static final ExecutorService pool = Executors.newCachedThreadPool();
# Line 21 | Line 25 | public final class CheckedLockLoops {
25      public static void main(String[] args) throws Exception {
26          int maxThreads = 100;
27          int iters = 2000000;
28 <        if (args.length > 0)
28 >        if (args.length > 0)
29              maxThreads = Integer.parseInt(args[0]);
30          rng.setSeed(3122688L);
31          warmup(iters);
# Line 31 | Line 35 | public final class CheckedLockLoops {
35  
36      static void runTest(int maxThreads, int iters) throws Exception {
37          print = true;
38 <        int k = 1;
35 <        for (int i = 1; i <= maxThreads;) {
38 >        for (int k = 1, i = 1; i <= maxThreads;) {
39              System.out.println("Threads:" + i);
40              oneTest(i, iters / i);
41              if (i == k) {
42                  k = i << 1;
43                  i = i + (i >>> 1);
44 <            }
45 <            else
44 >            }
45 >            else
46                  i = k;
47          }
48 <    }    
48 >    }
49  
50      static void warmup(int iters) throws Exception {
51          print = false;
# Line 52 | Line 55 | public final class CheckedLockLoops {
55      }
56  
57      static void oneTest(int nthreads, int iters) throws Exception {
58 <        int fairIters = (nthreads <= 1)? iters : iters/20;
58 >        int fairIters = (nthreads <= 1) ? iters : iters/20;
59          int v = rng.next();
60  
61          if (print)
62              System.out.print("NoLock (1 thread)     ");
63          new NoLockLoop().test(v, 1, iters * nthreads);
64          Thread.sleep(10);
65 <
65 >
66          if (print)
67              System.out.print("ReentrantLock         ");
68          new ReentrantLockLoop().test(v, nthreads, iters);
# Line 91 | Line 94 | public final class CheckedLockLoops {
94              System.out.print("Semaphore             ");
95          new SemaphoreLoop().test(v, nthreads, iters);
96          Thread.sleep(10);
97 <        
97 >
98          if (print)
99              System.out.print("FairSemaphore         ");
100          new FairSemaphoreLoop().test(v, nthreads, fairIters);
# Line 106 | Line 109 | public final class CheckedLockLoops {
109              System.out.print("FairRWriteLock         ");
110          new FairReentrantWriteLockLoop().test(v, nthreads, fairIters);
111          Thread.sleep(10);
112 <        
112 >
113          if (print)
114              System.out.print("ReentrantReadWriteLock");
115          new ReentrantReadWriteLockLoop().test(v, nthreads, iters);
116          Thread.sleep(10);
117 <        
117 >
118          if (print)
119              System.out.print("FairRReadWriteLock     ");
120          new FairReentrantReadWriteLockLoop().test(v, nthreads, fairIters);
121          Thread.sleep(10);
122      }
123  
124 <    static abstract class LockLoop implements Runnable {
124 >    abstract static class LockLoop implements Runnable {
125          int value;
126          int checkValue;
127          int iters;
# Line 135 | Line 138 | public final class CheckedLockLoops {
138  
139          final int getValue() {
140              int v = value;
141 <            if (checkValue != ~(v ^ 0xAAAAAAAA))
141 >            if (checkValue != ~(v ^ 0xAAAAAAAA))
142                  ++failures;
143              return v;
144          }
# Line 144 | Line 147 | public final class CheckedLockLoops {
147              setValue(initialValue);
148              this.iters = iters;
149              barrier = new CyclicBarrier(nthreads+1, timer);
150 <            for (int i = 0; i < nthreads; ++i)
150 >            for (int i = 0; i < nthreads; ++i)
151                  pool.execute(this);
152              barrier.await();
153              barrier.await();
# Line 164 | Line 167 | public final class CheckedLockLoops {
167          abstract int loop(int n);
168          public final void run() {
169              try {
170 <                barrier.await();
170 >                barrier.await();
171                  result += loop(iters);
172                  barrier.await();
173              }
174 <            catch (Exception ie) {
175 <                return;
174 >            catch (Exception ie) {
175 >                return;
176              }
177          }
178  
# Line 179 | Line 182 | public final class CheckedLockLoops {
182          private volatile int readBarrier;
183          final int loop(int n) {
184              int sum = 0;
185 <            int x = 0;;
185 >            int x = 0;
186              while (n-- > 0) {
187                  int r1 = readBarrier;
188                  x = setValue(LoopHelpers.compute1(getValue()));
# Line 195 | Line 198 | public final class CheckedLockLoops {
198      private static class BuiltinLockLoop extends LockLoop {
199          final int loop(int n) {
200              int sum = 0;
201 <            int x = 0;;
201 >            int x = 0;
202              while (n-- > 0) {
203 <                synchronized(this) {
203 >                synchronized (this) {
204                      x = setValue(LoopHelpers.compute1(getValue()));
205                  }
206                  sum += LoopHelpers.compute2(x);
# Line 207 | Line 210 | public final class CheckedLockLoops {
210      }
211  
212      private static class ReentrantLockLoop extends LockLoop {
213 <        final private ReentrantLock lock = new ReentrantLock();
213 >        private final ReentrantLock lock = new ReentrantLock();
214          final int loop(int n) {
215              final ReentrantLock lock = this.lock;
216              int sum = 0;
# Line 227 | Line 230 | public final class CheckedLockLoops {
230      }
231  
232      private static class MutexLoop extends LockLoop {
233 <        final private Mutex lock = new Mutex();
233 >        private final Mutex lock = new Mutex();
234          final int loop(int n) {
235              final Mutex lock = this.lock;
236              int sum = 0;
# Line 247 | Line 250 | public final class CheckedLockLoops {
250      }
251  
252      private static class LongMutexLoop extends LockLoop {
253 <        final private LongMutex lock = new LongMutex();
253 >        private final LongMutex lock = new LongMutex();
254          final int loop(int n) {
255              final LongMutex lock = this.lock;
256              int sum = 0;
# Line 267 | Line 270 | public final class CheckedLockLoops {
270      }
271  
272      private static class FairReentrantLockLoop extends LockLoop {
273 <        final private ReentrantLock lock = new ReentrantLock(true);
273 >        private final ReentrantLock lock = new ReentrantLock(true);
274          final int loop(int n) {
275              final ReentrantLock lock = this.lock;
276              int sum = 0;
# Line 287 | Line 290 | public final class CheckedLockLoops {
290      }
291  
292      private static class ReentrantWriteLockLoop extends LockLoop {
293 <        final private Lock lock = new ReentrantReadWriteLock().writeLock();
293 >        private final Lock lock = new ReentrantReadWriteLock().writeLock();
294          final int loop(int n) {
295              final Lock lock = this.lock;
296              int sum = 0;
# Line 327 | Line 330 | public final class CheckedLockLoops {
330      }
331  
332      private static class SemaphoreLoop extends LockLoop {
333 <        final private Semaphore sem = new Semaphore(1, false);
333 >        private final Semaphore sem = new Semaphore(1, false);
334          final int loop(int n) {
335              final Semaphore sem = this.sem;
336              int sum = 0;
# Line 346 | Line 349 | public final class CheckedLockLoops {
349          }
350      }
351      private static class FairSemaphoreLoop extends LockLoop {
352 <        final private Semaphore sem = new Semaphore(1, true);
352 >        private final Semaphore sem = new Semaphore(1, true);
353          final int loop(int n) {
354              final Semaphore sem = this.sem;
355              int sum = 0;
# Line 366 | Line 369 | public final class CheckedLockLoops {
369      }
370  
371      private static class ReentrantReadWriteLockLoop extends LockLoop {
372 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
372 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
373          final int loop(int n) {
374              final Lock rlock = lock.readLock();
375              final Lock wlock = lock.writeLock();
# Line 399 | Line 402 | public final class CheckedLockLoops {
402  
403      }
404  
402
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              final Lock rlock = lock.readLock();
409              final Lock wlock = lock.writeLock();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines