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.11 by jsr166, Mon Aug 10 03:13:33 2015 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 doFair = true;
22  
23      public static void main(String[] args) throws Exception {
24          int maxThreads = 100;
25          int iters = 1000000;
26          int replications = 1;
27  
28 <        if (args.length > 0)
28 >        if (args.length > 0)
29              maxThreads = Integer.parseInt(args[0]);
30  
31 <        if (args.length > 1)
31 >        if (args.length > 1)
32              iters = Integer.parseInt(args[1]);
33  
34 <        if (args.length > 2)
34 >        if (args.length > 2)
35              replications = Integer.parseInt(args[2]);
36  
37          rng.setSeed(3122688L);
# Line 88 | Line 93 | public final class LockLoops {
93                  System.out.print("ReentrantReadWriteLock");
94              new ReentrantReadWriteLockLoop().test(v, nthreads, iters);
95              Thread.sleep(10);
91
96          }
97  
98          if (doSemaphore) {
# Line 101 | Line 105 | public final class LockLoops {
105                  System.out.print("FairSemaphore         ");
106              new FairSemaphoreLoop().test(v, nthreads, iters);
107              Thread.sleep(10);
104
108          }
109  
110          if (doFair) {
# Line 121 | Line 124 | public final class LockLoops {
124                  new FairReentrantReadWriteLockLoop().test(v, nthreads, iters);
125                  Thread.sleep(10);
126              }
124
127          }
126
128      }
129  
130 <    static abstract class LockLoop implements Runnable {
130 >    abstract static class LockLoop implements Runnable {
131          int v;
132          int iters;
133          volatile int result;
# Line 137 | Line 138 | public final class LockLoops {
138              v = initialValue;
139              this.iters = iters;
140              barrier = new CyclicBarrier(nthreads+1, timer);
141 <            for (int i = 0; i < nthreads; ++i)
141 >            for (int i = 0; i < nthreads; ++i)
142                  pool.execute(this);
143              barrier.await();
144              barrier.await();
# Line 145 | Line 146 | public final class LockLoops {
146              if (print) {
147                  long tpi = time / (iters * nthreads);
148                  System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update");
149 <                //                double secs = (double)(time) / 1000000000.0;
149 >                //                double secs = (double) time / 1000000000.0;
150                  //                System.out.print("\t " + secs + "s run time");
151                  System.out.println();
152              }
# Line 156 | Line 157 | public final class LockLoops {
157          abstract int loop(int n);
158          public final void run() {
159              try {
160 <                barrier.await();
160 >                barrier.await();
161                  result += loop(iters);
162                  barrier.await();
163              }
164 <            catch (Exception ie) {
165 <                return;
164 >            catch (Exception ie) {
165 >                return;
166              }
167          }
168  
# Line 171 | Line 172 | public final class LockLoops {
172          final int loop(int n) {
173              int sum = 0;
174              while (n-- > 0) {
175 <                synchronized(this) {
175 >                synchronized (this) {
176                      v = LoopHelpers.compute1(v);
177                  }
178                  sum += LoopHelpers.compute2(v);
# Line 193 | Line 194 | public final class LockLoops {
194      }
195  
196      private static class NoLockVolatileLoop extends LockLoop {
197 <        volatile private int vv;
197 >        private volatile int vv;
198          final int loop(int n) {
199              int sum = 0;
200              while (n-- > 0) {
# Line 206 | Line 207 | public final class LockLoops {
207      }
208  
209      private static class ReentrantLockLoop extends LockLoop {
210 <        final private ReentrantLock lock = new ReentrantLock();
210 >        private final ReentrantLock lock = new ReentrantLock();
211          final int loop(int n) {
212              int sum = 0;
213              while (n-- > 0) {
# Line 224 | Line 225 | public final class LockLoops {
225      }
226  
227      private static class FairReentrantLockLoop extends LockLoop {
228 <        final private ReentrantLock lock = new ReentrantLock(true);
228 >        private final ReentrantLock lock = new ReentrantLock(true);
229          final int loop(int n) {
230              int sum = 0;
231              while (n-- > 0) {
# Line 242 | Line 243 | public final class LockLoops {
243      }
244  
245      private static class ReentrantWriteLockLoop extends LockLoop {
246 <        final private Lock lock = new ReentrantReadWriteLock().writeLock();
246 >        private final Lock lock = new ReentrantReadWriteLock().writeLock();
247          final int loop(int n) {
248              int sum = 0;
249              while (n-- > 0) {
# Line 260 | Line 261 | public final class LockLoops {
261      }
262  
263      private static class ReentrantReadWriteLockLoop extends LockLoop {
264 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
264 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
265          final int loop(int n) {
266              int sum = 0;
267              while (n-- > 0) {
# Line 304 | Line 305 | public final class LockLoops {
305      }
306  
307      private static class SemaphoreLoop extends LockLoop {
308 <        final private Semaphore sem = new Semaphore(1, false);
308 >        private final Semaphore sem = new Semaphore(1, false);
309          final int loop(int n) {
310              int sum = 0;
311              try {
# Line 326 | Line 327 | public final class LockLoops {
327          }
328      }
329      private static class FairSemaphoreLoop extends LockLoop {
330 <        final private Semaphore sem = new Semaphore(1, true);
330 >        private final Semaphore sem = new Semaphore(1, true);
331          final int loop(int n) {
332              int sum = 0;
333              try {
# Line 349 | Line 350 | public final class LockLoops {
350      }
351  
352      private static class FairReentrantReadWriteLockLoop extends LockLoop {
353 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
353 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
354          final int loop(int n) {
355              int sum = 0;
356              while (n-- > 0) {
# Line 373 | Line 374 | public final class LockLoops {
374              return sum;
375          }
376      }
376
377
377   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines