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