ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutMutexLoops.java
Revision: 1.8
Committed: Sat Dec 31 19:29:58 2016 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +0 -1 lines
Log Message:
organize imports

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