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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
import java.util.Arrays; |
8 |
import java.util.Collections; |
9 |
import java.util.Random; |
10 |
import java.util.concurrent.BrokenBarrierException; |
11 |
import java.util.concurrent.CyclicBarrier; |
12 |
import java.util.concurrent.locks.ReentrantLock; |
13 |
|
14 |
public final class CancelledLockLoops { |
15 |
static final Random rng = new Random(); |
16 |
static boolean print = false; |
17 |
static final int ITERS = 10000000; |
18 |
static final long TIMEOUT = 100; |
19 |
|
20 |
public static void main(String[] args) throws Exception { |
21 |
int maxThreads = 100; |
22 |
if (args.length > 0) |
23 |
maxThreads = Integer.parseInt(args[0]); |
24 |
|
25 |
print = true; |
26 |
|
27 |
for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) { |
28 |
System.out.print("Threads: " + i); |
29 |
try { |
30 |
new ReentrantLockLoop(i).test(); |
31 |
} |
32 |
catch (BrokenBarrierException bb) { |
33 |
// OK, ignore |
34 |
} |
35 |
} |
36 |
} |
37 |
|
38 |
static final class ReentrantLockLoop implements Runnable { |
39 |
private int v = rng.nextInt(); |
40 |
private int 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 |
Thread[] threads = new Thread[nthreads]; |
53 |
for (int i = 0; i < threads.length; ++i) |
54 |
threads[i] = new Thread(this); |
55 |
for (int i = 0; i < threads.length; ++i) |
56 |
threads[i].start(); |
57 |
Thread[] cancels = threads.clone(); |
58 |
Collections.shuffle(Arrays.asList(cancels), rng); |
59 |
barrier.await(); |
60 |
Thread.sleep(TIMEOUT); |
61 |
for (int i = 0; i < cancels.length-2; ++i) { |
62 |
cancels[i].interrupt(); |
63 |
// make sure all OK even when cancellations spaced out |
64 |
if ( (i & 3) == 0) |
65 |
Thread.sleep(1 + rng.nextInt(10)); |
66 |
} |
67 |
barrier.await(); |
68 |
if (print) { |
69 |
long time = timer.getTime(); |
70 |
double secs = (double) time / 1000000000.0; |
71 |
System.out.println("\t " + secs + "s run time"); |
72 |
} |
73 |
|
74 |
int c; |
75 |
lock.lock(); |
76 |
try { |
77 |
c = completed; |
78 |
} |
79 |
finally { |
80 |
lock.unlock(); |
81 |
} |
82 |
if (c != 2) |
83 |
throw new Error("Completed != 2"); |
84 |
int r = result; |
85 |
if (r == 0) // avoid overoptimization |
86 |
System.out.println("useless result: " + r); |
87 |
} |
88 |
|
89 |
public final void run() { |
90 |
try { |
91 |
barrier.await(); |
92 |
int sum = v; |
93 |
int x = 0; |
94 |
int n = ITERS; |
95 |
boolean done = false; |
96 |
do { |
97 |
try { |
98 |
lock.lockInterruptibly(); |
99 |
} |
100 |
catch (InterruptedException ie) { |
101 |
break; |
102 |
} |
103 |
try { |
104 |
v = x = LoopHelpers.compute1(v); |
105 |
} |
106 |
finally { |
107 |
lock.unlock(); |
108 |
} |
109 |
sum += LoopHelpers.compute2(x); |
110 |
} while (n-- > 0); |
111 |
if (n <= 0) { |
112 |
lock.lock(); |
113 |
try { |
114 |
++completed; |
115 |
} |
116 |
finally { |
117 |
lock.unlock(); |
118 |
} |
119 |
} |
120 |
barrier.await(); |
121 |
result += sum; |
122 |
} |
123 |
catch (Exception ex) { |
124 |
// ex.printStackTrace(); |
125 |
return; |
126 |
} |
127 |
} |
128 |
} |
129 |
|
130 |
} |