ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutMutexLoops.java
Revision: 1.5
Committed: Tue Mar 15 19:47:06 2011 UTC (13 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.4: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

File Contents

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