ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/LockOncePerThreadLoops.java
Revision: 1.1
Committed: Mon May 2 19:19:38 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Put misc performance tests into CVS

File Contents

# User Rev Content
1 dl 1.1 /*
2     * @test
3     * @summary Checks for missed signals by locking and unlocking each of an array of locks once per thread
4     */
5     /*
6     * Written by Doug Lea with assistance from members of JCP JSR-166
7     * Expert Group and released to the public domain. Use, modify, and
8     * redistribute this code in any way without acknowledgement.
9     */
10    
11     import java.util.concurrent.*;
12     import java.util.concurrent.locks.*;
13     import java.util.*;
14    
15     public final class LockOncePerThreadLoops {
16     static final ExecutorService pool = Executors.newCachedThreadPool();
17     static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
18     static boolean print = false;
19     static int nlocks = 500000;
20     static int nthreads = 100;
21     static int replications = 20;
22    
23     public static void main(String[] args) throws Exception {
24     if (args.length > 0)
25     replications = Integer.parseInt(args[0]);
26    
27     if (args.length > 1)
28     nlocks = Integer.parseInt(args[1]);
29    
30     print = true;
31    
32     for (int i = 0; i < replications; ++i) {
33     System.out.print("Iteration: " + i);
34     new ReentrantLockLoop().test();
35     Thread.sleep(100);
36     }
37     pool.shutdown();
38     }
39    
40     static final class ReentrantLockLoop implements Runnable {
41     private int v = rng.next();
42     private volatile int result = 17;
43     final ReentrantLock[]locks = new ReentrantLock[nlocks];
44    
45     private final ReentrantLock lock = new ReentrantLock();
46     private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
47     private final CyclicBarrier barrier;
48     ReentrantLockLoop() {
49     barrier = new CyclicBarrier(nthreads+1, timer);
50     for (int i = 0; i < nlocks; ++i)
51     locks[i] = new ReentrantLock();
52     }
53    
54     final void test() throws Exception {
55     for (int i = 0; i < nthreads; ++i)
56     pool.execute(this);
57     barrier.await();
58     barrier.await();
59     if (print) {
60     long time = timer.getTime();
61     double secs = (double)(time) / 1000000000.0;
62     System.out.println("\t " + secs + "s run time");
63     }
64    
65     int r = result;
66     if (r == 0) // avoid overoptimization
67     System.out.println("useless result: " + r);
68     }
69    
70     public final void run() {
71     try {
72     barrier.await();
73     int sum = v;
74     int x = 0;
75     for (int i = 0; i < locks.length; ++i) {
76     locks[i].lock();
77     try {
78     v = x += ~(v - i);
79     }
80     finally {
81     locks[i].unlock();
82     }
83     // Once in a while, do something more expensive
84     if ((~i & 255) == 0) {
85     sum += LoopHelpers.compute1(LoopHelpers.compute2(x));
86     }
87     else
88     sum += sum ^ x;
89     }
90     barrier.await();
91     result += sum;
92     }
93     catch (Exception ie) {
94     return;
95     }
96     }
97     }
98    
99     }