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

Comparing jsr166/src/test/loops/SimpleFairReentrantLockLoops.java (file contents):
Revision 1.2 by dl, Fri Jun 10 15:45:19 2005 UTC vs.
Revision 1.9 by jsr166, Sat Dec 31 02:28:19 2016 UTC

# Line 1 | Line 1
1   /*
2 * @test
3 * @synopsis multiple threads using a single lock
4 */
5 /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain. Use, modify, and
4 < * redistribute this code in any way without acknowledgement.
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import java.util.concurrent.*;
8 < import java.util.concurrent.locks.*;
9 < import java.util.*;
7 > import java.util.concurrent.CyclicBarrier;
8 > import java.util.concurrent.ExecutorService;
9 > import java.util.concurrent.Executors;
10 > import java.util.concurrent.locks.Lock;
11 > import java.util.concurrent.locks.ReentrantLock;
12  
13   public final class SimpleFairReentrantLockLoops {
14      static final ExecutorService pool = Executors.newCachedThreadPool();
# Line 20 | Line 18 | public final class SimpleFairReentrantLo
18  
19      public static void main(String[] args) throws Exception {
20          int maxThreads = 100;
21 <        if (args.length > 0)
21 >        if (args.length > 0)
22              maxThreads = Integer.parseInt(args[0]);
23  
24          new ReentrantLockLoop(1).test();
25          new ReentrantLockLoop(1).test();
26          print = true;
27  
28 <        int k = 1;
31 <        for (int i = 1; i <= maxThreads;) {
28 >        for (int k = 1, i = 1; i <= maxThreads;) {
29              System.out.print("Threads: " + i);
30              new ReentrantLockLoop(i).test();
31              Thread.sleep(100);
32              if (i == k) {
33                  k = i << 1;
34                  i = i + (i >>> 1);
35 <            }
36 <            else
35 >            }
36 >            else
37                  i = k;
38          }
39          pool.shutdown();
# Line 56 | Line 53 | public final class SimpleFairReentrantLo
53          }
54  
55          final void test() throws Exception {
56 <            for (int i = 0; i < nthreads; ++i)
56 >            for (int i = 0; i < nthreads; ++i)
57                  pool.execute(this);
58              barrier.await();
59              barrier.await();
60              if (print) {
61                  long time = timer.getTime();
62 <                long tpi = time / ((long)iters * nthreads);
62 >                long tpi = time / ((long) iters * nthreads);
63                  System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock");
64 <                double secs = (double)(time) / 1000000000.0;
64 >                double secs = (double) time / 1000000000.0;
65                  System.out.println("\t " + secs + "s run time");
66              }
67  
# Line 76 | Line 73 | public final class SimpleFairReentrantLo
73          public final void run() {
74              final ReentrantLock lock = this.lock;
75              try {
76 <                barrier.await();
76 >                barrier.await();
77                  int sum = v + 1;
78                  int x = 0;
79                  int n = iters;
# Line 95 | Line 92 | public final class SimpleFairReentrantLo
92                          ++readBarrier;
93                      for (int l = x & 7; l > 0; --l)
94                          sum += LoopHelpers.compute6(sum);
95 <                }
95 >                }
96                  barrier.await();
97                  result += sum;
98              }
99 <            catch (Exception ie) {
100 <                return;
99 >            catch (Exception ie) {
100 >                return;
101              }
102          }
103      }
104  
105   }
109

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines