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