ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutMutexLoops.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     * Checks for responsiveness of locks to timeouts. Runs under the
4     * assumption that ITERS computations require more than TIMEOUT msecs
5     * to complete, which seems to be a safe assumption for another
6     * decade.
7     */
8    
9     import java.util.concurrent.*;
10     import java.util.concurrent.locks.*;
11     import java.util.*;
12    
13     public final class TimeoutMutexLoops {
14     static final ExecutorService pool = Executors.newCachedThreadPool();
15     static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
16     static boolean print = false;
17     static final int ITERS = Integer.MAX_VALUE;
18     static final long TIMEOUT = 100;
19    
20     public static void main(String[] args) throws Exception {
21     int maxThreads = 100;
22     if (args.length > 0)
23     maxThreads = Integer.parseInt(args[0]);
24    
25     print = true;
26    
27     for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) {
28     System.out.print("Threads: " + i);
29     new MutexLoop(i).test();
30     Thread.sleep(TIMEOUT);
31     }
32     pool.shutdown();
33     }
34    
35     static final class MutexLoop implements Runnable {
36     private int v = rng.next();
37     private volatile boolean completed;
38     private volatile int result = 17;
39     private final Mutex lock = new Mutex();
40     private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
41     private final CyclicBarrier barrier;
42     private final int nthreads;
43     MutexLoop(int nthreads) {
44     this.nthreads = nthreads;
45     barrier = new CyclicBarrier(nthreads+1, timer);
46     }
47    
48     final void test() throws Exception {
49     for (int i = 0; i < nthreads; ++i)
50     pool.execute(this);
51     barrier.await();
52     Thread.sleep(TIMEOUT);
53     lock.lock();
54     barrier.await();
55     if (print) {
56     long time = timer.getTime();
57     double secs = (double)(time) / 1000000000.0;
58     System.out.println("\t " + secs + "s run time");
59     }
60    
61     if (completed)
62     throw new Error("Some thread completed instead of timing out");
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     try {
70     barrier.await();
71     int sum = v;
72     int x = 0;
73     int n = ITERS;
74     do {
75     if (!lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS))
76     break;
77     try {
78     v = x = LoopHelpers.compute1(v);
79     }
80     finally {
81     lock.unlock();
82     }
83     sum += LoopHelpers.compute2(x);
84     } while (n-- > 0);
85     if (n <= 0)
86     completed = true;
87     barrier.await();
88     result += sum;
89     }
90     catch (Exception ie) {
91     return;
92     }
93     }
94     }
95    
96     }