1 |
/* |
2 |
* @test |
3 |
* @summary tests lockInterruptibly |
4 |
* Checks for responsiveness of locks to interrupts. Runs under that |
5 |
* assumption that ITERS_VALUE computations require more than TIMEOUT |
6 |
* msecs to complete. |
7 |
*/ |
8 |
/* |
9 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
10 |
* Expert Group and released to the public domain. Use, modify, and |
11 |
* redistribute this code in any way without acknowledgement. |
12 |
*/ |
13 |
|
14 |
import java.util.concurrent.*; |
15 |
import java.util.concurrent.locks.*; |
16 |
import java.util.*; |
17 |
|
18 |
public final class CancelledLockLoops { |
19 |
static final Random rng = new Random(); |
20 |
static boolean print = false; |
21 |
static final int ITERS = 10000000; |
22 |
static final long TIMEOUT = 100; |
23 |
|
24 |
public static void main(String[] args) throws Exception { |
25 |
int maxThreads = 100; |
26 |
if (args.length > 0) |
27 |
maxThreads = Integer.parseInt(args[0]); |
28 |
|
29 |
print = true; |
30 |
|
31 |
for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) { |
32 |
System.out.print("Threads: " + i); |
33 |
try { |
34 |
new ReentrantLockLoop(i).test(); |
35 |
} |
36 |
catch(BrokenBarrierException bb) { |
37 |
// OK, ignore |
38 |
} |
39 |
} |
40 |
} |
41 |
|
42 |
static final class ReentrantLockLoop implements Runnable { |
43 |
private int v = rng.nextInt(); |
44 |
private int completed; |
45 |
private volatile int result = 17; |
46 |
private final ReentrantLock lock = new ReentrantLock(); |
47 |
private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
48 |
private final CyclicBarrier barrier; |
49 |
private final int nthreads; |
50 |
ReentrantLockLoop(int nthreads) { |
51 |
this.nthreads = nthreads; |
52 |
barrier = new CyclicBarrier(nthreads+1, timer); |
53 |
} |
54 |
|
55 |
final void test() throws Exception { |
56 |
Thread[] threads = new Thread[nthreads]; |
57 |
for (int i = 0; i < threads.length; ++i) |
58 |
threads[i] = new Thread(this); |
59 |
for (int i = 0; i < threads.length; ++i) |
60 |
threads[i].start(); |
61 |
Thread[] cancels = (Thread[]) (threads.clone()); |
62 |
Collections.shuffle(Arrays.asList(cancels), rng); |
63 |
barrier.await(); |
64 |
Thread.sleep(TIMEOUT); |
65 |
for (int i = 0; i < cancels.length-2; ++i) { |
66 |
cancels[i].interrupt(); |
67 |
// make sure all OK even when cancellations spaced out |
68 |
if ( (i & 3) == 0) |
69 |
Thread.sleep(1 + rng.nextInt(10)); |
70 |
} |
71 |
barrier.await(); |
72 |
if (print) { |
73 |
long time = timer.getTime(); |
74 |
double secs = (double)(time) / 1000000000.0; |
75 |
System.out.println("\t " + secs + "s run time"); |
76 |
} |
77 |
|
78 |
int c; |
79 |
lock.lock(); |
80 |
try { |
81 |
c = completed; |
82 |
} |
83 |
finally { |
84 |
lock.unlock(); |
85 |
} |
86 |
if (completed != 2) |
87 |
throw new Error("Completed != 2"); |
88 |
int r = result; |
89 |
if (r == 0) // avoid overoptimization |
90 |
System.out.println("useless result: " + r); |
91 |
} |
92 |
|
93 |
public final void run() { |
94 |
try { |
95 |
barrier.await(); |
96 |
int sum = v; |
97 |
int x = 0; |
98 |
int n = ITERS; |
99 |
boolean done = false; |
100 |
do { |
101 |
try { |
102 |
lock.lockInterruptibly(); |
103 |
} |
104 |
catch (InterruptedException ie) { |
105 |
break; |
106 |
} |
107 |
try { |
108 |
v = x = LoopHelpers.compute1(v); |
109 |
} |
110 |
finally { |
111 |
lock.unlock(); |
112 |
} |
113 |
sum += LoopHelpers.compute2(x); |
114 |
} while (n-- > 0); |
115 |
if (n <= 0) { |
116 |
lock.lock(); |
117 |
try { |
118 |
++completed; |
119 |
} |
120 |
finally { |
121 |
lock.unlock(); |
122 |
} |
123 |
} |
124 |
barrier.await(); |
125 |
result += sum; |
126 |
} |
127 |
catch (Exception ex) { |
128 |
// ex.printStackTrace(); |
129 |
return; |
130 |
} |
131 |
} |
132 |
} |
133 |
|
134 |
} |