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.2 by dl, Mon May 9 19:33:30 2005 UTC vs.
Revision 1.9 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   * 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.*;
12 import java.util.*;
13  
14   public final class LockLoops {
15      static final ExecutorService pool = Executors.newCachedThreadPool();
# Line 18 | 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 131 | Line 131 | public final class LockLoops {
131  
132      }
133  
134 <    static abstract class LockLoop implements Runnable {
134 >    abstract static class LockLoop implements Runnable {
135          int v;
136          int iters;
137          volatile int result;
# Line 142 | Line 142 | public final class LockLoops {
142              v = initialValue;
143              this.iters = iters;
144              barrier = new CyclicBarrier(nthreads+1, timer);
145 <            for (int i = 0; i < nthreads; ++i)
145 >            for (int i = 0; i < nthreads; ++i)
146                  pool.execute(this);
147              barrier.await();
148              barrier.await();
# Line 150 | Line 150 | public final class LockLoops {
150              if (print) {
151                  long tpi = time / (iters * nthreads);
152                  System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update");
153 <                //                double secs = (double)(time) / 1000000000.0;
153 >                //                double secs = (double) time / 1000000000.0;
154                  //                System.out.print("\t " + secs + "s run time");
155                  System.out.println();
156              }
# Line 161 | Line 161 | public final class LockLoops {
161          abstract int loop(int n);
162          public final void run() {
163              try {
164 <                barrier.await();
164 >                barrier.await();
165                  result += loop(iters);
166                  barrier.await();
167              }
168 <            catch (Exception ie) {
169 <                return;
168 >            catch (Exception ie) {
169 >                return;
170              }
171          }
172  
# Line 176 | Line 176 | public final class LockLoops {
176          final int loop(int n) {
177              int sum = 0;
178              while (n-- > 0) {
179 <                synchronized(this) {
179 >                synchronized (this) {
180                      v = LoopHelpers.compute1(v);
181                  }
182                  sum += LoopHelpers.compute2(v);
# Line 198 | Line 198 | public final class LockLoops {
198      }
199  
200      private static class NoLockVolatileLoop extends LockLoop {
201 <        volatile private int vv;
201 >        private volatile int vv;
202          final int loop(int n) {
203              int sum = 0;
204              while (n-- > 0) {
# Line 211 | Line 211 | public final class LockLoops {
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              int sum = 0;
217              while (n-- > 0) {
# Line 229 | Line 229 | public final class LockLoops {
229      }
230  
231      private static class FairReentrantLockLoop extends LockLoop {
232 <        final private ReentrantLock lock = new ReentrantLock(true);
232 >        private final ReentrantLock lock = new ReentrantLock(true);
233          final int loop(int n) {
234              int sum = 0;
235              while (n-- > 0) {
# Line 247 | Line 247 | public final class LockLoops {
247      }
248  
249      private static class ReentrantWriteLockLoop extends LockLoop {
250 <        final private Lock lock = new ReentrantReadWriteLock().writeLock();
250 >        private final Lock lock = new ReentrantReadWriteLock().writeLock();
251          final int loop(int n) {
252              int sum = 0;
253              while (n-- > 0) {
# Line 265 | Line 265 | public final class LockLoops {
265      }
266  
267      private static class ReentrantReadWriteLockLoop extends LockLoop {
268 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
268 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
269          final int loop(int n) {
270              int sum = 0;
271              while (n-- > 0) {
# Line 309 | Line 309 | public final class LockLoops {
309      }
310  
311      private static class SemaphoreLoop extends LockLoop {
312 <        final private Semaphore sem = new Semaphore(1, false);
312 >        private final Semaphore sem = new Semaphore(1, false);
313          final int loop(int n) {
314              int sum = 0;
315              try {
# Line 331 | Line 331 | public final class LockLoops {
331          }
332      }
333      private static class FairSemaphoreLoop extends LockLoop {
334 <        final private Semaphore sem = new Semaphore(1, true);
334 >        private final Semaphore sem = new Semaphore(1, true);
335          final int loop(int n) {
336              int sum = 0;
337              try {
# Line 354 | Line 354 | public final class LockLoops {
354      }
355  
356      private static class FairReentrantReadWriteLockLoop extends LockLoop {
357 <        final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
357 >        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
358          final int loop(int n) {
359              int sum = 0;
360              while (n-- > 0) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines