ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutLockLoops.java
Revision: 1.10
Committed: Sat Dec 31 18:54:28 2016 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.9: +5 -3 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.6 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.2 */
6     /*
7 jsr166 1.3 * @test
8 dl 1.1 * @bug 4486658
9     * @run main TimeoutLockLoops
10     * @summary Checks for responsiveness of locks to timeouts.
11     * Runs under the assumption that ITERS computations require more than
12     * TIMEOUT msecs to complete, which seems to be a safe assumption for
13     * another decade.
14     */
15    
16 jsr166 1.10 import java.util.concurrent.CyclicBarrier;
17     import java.util.concurrent.ExecutorService;
18     import java.util.concurrent.Executors;
19     import java.util.concurrent.TimeUnit;
20     import java.util.concurrent.locks.ReentrantLock;
21 dl 1.1
22     public final class TimeoutLockLoops {
23     static final ExecutorService pool = Executors.newCachedThreadPool();
24     static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
25     static boolean print = false;
26     static final int ITERS = Integer.MAX_VALUE;
27 jsr166 1.4 static final long TIMEOUT = 100;
28 dl 1.1
29     public static void main(String[] args) throws Exception {
30     int maxThreads = 100;
31 jsr166 1.4 if (args.length > 0)
32 dl 1.1 maxThreads = Integer.parseInt(args[0]);
33    
34     print = true;
35    
36     for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) {
37     System.out.print("Threads: " + i);
38     new ReentrantLockLoop(i).test();
39     // Thread.sleep(10);
40     }
41     pool.shutdown();
42     }
43    
44     static final class ReentrantLockLoop implements Runnable {
45     private int v = rng.next();
46     private volatile boolean completed;
47     private volatile int result = 17;
48     private final ReentrantLock lock = new ReentrantLock();
49     private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
50     private final CyclicBarrier barrier;
51     private final int nthreads;
52     ReentrantLockLoop(int nthreads) {
53     this.nthreads = nthreads;
54     barrier = new CyclicBarrier(nthreads+1, timer);
55     }
56    
57     final void test() throws Exception {
58     for (int i = 0; i < nthreads; ++i) {
59     lock.lock();
60     pool.execute(this);
61     lock.unlock();
62     }
63     barrier.await();
64     Thread.sleep(TIMEOUT);
65     while (!lock.tryLock()); // Jam lock
66     // lock.lock();
67     barrier.await();
68     if (print) {
69     long time = timer.getTime();
70 jsr166 1.5 double secs = (double) time / 1000000000.0;
71 dl 1.1 System.out.println("\t " + secs + "s run time");
72     }
73    
74     if (completed)
75     throw new Error("Some thread completed instead of timing out");
76     int r = result;
77     if (r == 0) // avoid overoptimization
78     System.out.println("useless result: " + r);
79     }
80    
81     public final void run() {
82     try {
83 jsr166 1.4 barrier.await();
84 dl 1.1 int sum = v;
85     int x = 17;
86     int n = ITERS;
87     final ReentrantLock lock = this.lock;
88     for (;;) {
89     if (x != 0) {
90     if (n-- <= 0)
91     break;
92     }
93     if (!lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS))
94     break;
95     try {
96     v = x = LoopHelpers.compute1(v);
97     }
98     finally {
99     lock.unlock();
100     }
101     sum += LoopHelpers.compute2(x);
102     }
103     if (n <= 0)
104     completed = true;
105     barrier.await();
106     result += sum;
107     }
108 jsr166 1.4 catch (Exception ex) {
109 dl 1.1 ex.printStackTrace();
110 jsr166 1.4 return;
111 dl 1.1 }
112     }
113     }
114     }