ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SimpleTimedLockLoops.java
Revision: 1.3
Committed: Mon Feb 19 00:46:06 2007 UTC (17 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.2: +2 -6 lines
Log Message:
Uniform headers

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.3 * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 dl 1.1 */
6    
7     import java.util.concurrent.*;
8     import java.util.concurrent.locks.*;
9     import java.util.*;
10    
11     public final class SimpleTimedLockLoops {
12     static final ExecutorService pool = Executors.newCachedThreadPool();
13     static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
14     static boolean print = false;
15     static int iters = 10000000;
16    
17     public static void main(String[] args) throws Exception {
18     int maxThreads = 100;
19     if (args.length > 0)
20     maxThreads = Integer.parseInt(args[0]);
21    
22     print = true;
23    
24     int reps = 3;
25     for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) {
26     int n = reps;
27     if (reps > 1) --reps;
28     while (n-- > 0) {
29     System.out.print("Threads: " + i);
30     new ReentrantLockLoop(i).test();
31     Thread.sleep(100);
32     }
33     }
34     pool.shutdown();
35     }
36    
37     static final class ReentrantLockLoop implements Runnable {
38     private int v = rng.next();
39     private volatile int result = 17;
40     private final ReentrantLock lock = new ReentrantLock();
41     private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
42     private final CyclicBarrier barrier;
43     private final int nthreads;
44     private volatile int readBarrier;
45     ReentrantLockLoop(int nthreads) {
46     this.nthreads = nthreads;
47     barrier = new CyclicBarrier(nthreads+1, timer);
48     }
49    
50     final void test() throws Exception {
51     for (int i = 0; i < nthreads; ++i)
52     pool.execute(this);
53     barrier.await();
54     barrier.await();
55     if (print) {
56     long time = timer.getTime();
57     long tpi = time / ((long)iters * nthreads);
58     System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock");
59     double secs = (double)(time) / 1000000000.0;
60     System.out.println("\t " + secs + "s run time");
61     }
62    
63     int r = result;
64     if (r == 0) // avoid overoptimization
65     System.out.println("useless result: " + r);
66     }
67    
68     public final void run() {
69     final ReentrantLock lock = this.lock;
70     try {
71     barrier.await();
72     int sum = v + 1;
73     int x = 0;
74     int n = iters;
75     while (n > 0) {
76     if (lock.tryLock(1, TimeUnit.SECONDS)) {
77     --n;
78     int k = (sum & 3);
79     if (k > 0) {
80     x = v;
81     while (k-- > 0)
82 dl 1.2 x = LoopHelpers.compute6(x);
83 dl 1.1 v = x;
84     }
85     else x = sum + 1;
86     lock.unlock();
87     }
88     if ((x += readBarrier) == 0)
89     ++readBarrier;
90     for (int l = x & 7; l > 0; --l)
91 dl 1.2 sum += LoopHelpers.compute6(sum);
92 dl 1.1 }
93     barrier.await();
94     result += sum;
95     }
96     catch (Exception ie) {
97     return;
98     }
99     }
100     }
101     }