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