1 |
dl |
1.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 |
dl |
1.8 |
import java.util.*; |
7 |
|
|
import java.util.concurrent.Executor; |
8 |
|
|
import java.util.concurrent.Executors; |
9 |
|
|
import java.util.concurrent.ExecutorService; |
10 |
|
|
import java.util.concurrent.AbstractExecutorService; |
11 |
|
|
import java.util.concurrent.CountDownLatch; |
12 |
|
|
import java.util.concurrent.Callable; |
13 |
|
|
import java.util.concurrent.Future; |
14 |
|
|
import java.util.concurrent.ExecutionException; |
15 |
|
|
import java.util.concurrent.CancellationException; |
16 |
|
|
import java.util.concurrent.RejectedExecutionException; |
17 |
|
|
import java.util.concurrent.ForkJoinPool; |
18 |
|
|
import java.util.concurrent.ForkJoinTask; |
19 |
|
|
import java.util.concurrent.ForkJoinWorkerThread; |
20 |
|
|
import java.util.concurrent.RecursiveAction; |
21 |
|
|
import java.util.concurrent.RecursiveTask; |
22 |
|
|
import java.util.concurrent.TimeUnit; |
23 |
dl |
1.1 |
import junit.framework.*; |
24 |
dl |
1.8 |
import java.util.concurrent.TimeUnit; |
25 |
dl |
1.1 |
import java.util.concurrent.atomic.*; |
26 |
|
|
import java.util.*; |
27 |
|
|
|
28 |
|
|
public class ForkJoinTaskTest extends JSR166TestCase { |
29 |
|
|
|
30 |
|
|
public static void main(String[] args) { |
31 |
jsr166 |
1.9 |
junit.textui.TestRunner.run(suite()); |
32 |
dl |
1.1 |
} |
33 |
|
|
public static Test suite() { |
34 |
jsr166 |
1.5 |
return new TestSuite(ForkJoinTaskTest.class); |
35 |
dl |
1.1 |
} |
36 |
|
|
|
37 |
|
|
/** |
38 |
|
|
* Testing coverage notes: |
39 |
jsr166 |
1.2 |
* |
40 |
dl |
1.1 |
* To test extension methods and overrides, most tests use |
41 |
|
|
* BinaryAsyncAction extension class that processes joins |
42 |
|
|
* differently than supplied Recursive forms. |
43 |
jsr166 |
1.2 |
*/ |
44 |
dl |
1.1 |
|
45 |
|
|
static final ForkJoinPool mainPool = new ForkJoinPool(); |
46 |
|
|
static final ForkJoinPool singletonPool = new ForkJoinPool(1); |
47 |
jsr166 |
1.10 |
static final ForkJoinPool asyncSingletonPool = |
48 |
dl |
1.8 |
new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
49 |
|
|
null, true); |
50 |
dl |
1.1 |
static final class FJException extends RuntimeException { |
51 |
|
|
FJException() { super(); } |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
static abstract class BinaryAsyncAction extends ForkJoinTask<Void> { |
55 |
|
|
private volatile int controlState; |
56 |
|
|
|
57 |
|
|
static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater = |
58 |
jsr166 |
1.2 |
AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, |
59 |
dl |
1.1 |
"controlState"); |
60 |
|
|
|
61 |
|
|
private BinaryAsyncAction parent; |
62 |
|
|
|
63 |
|
|
private BinaryAsyncAction sibling; |
64 |
|
|
|
65 |
|
|
protected BinaryAsyncAction() { |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
public final Void getRawResult() { return null; } |
69 |
|
|
protected final void setRawResult(Void mustBeNull) { } |
70 |
|
|
|
71 |
|
|
public final void linkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) { |
72 |
|
|
x.parent = y.parent = this; |
73 |
|
|
x.sibling = y; |
74 |
|
|
y.sibling = x; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) { |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
protected boolean onException() { |
81 |
|
|
return true; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
public void linkAndForkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) { |
85 |
|
|
linkSubtasks(x, y); |
86 |
|
|
y.fork(); |
87 |
|
|
x.fork(); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
private void completeThis() { |
91 |
|
|
super.complete(null); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
private void completeThisExceptionally(Throwable ex) { |
95 |
|
|
super.completeExceptionally(ex); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
public final void complete() { |
99 |
|
|
BinaryAsyncAction a = this; |
100 |
|
|
for (;;) { |
101 |
|
|
BinaryAsyncAction s = a.sibling; |
102 |
|
|
BinaryAsyncAction p = a.parent; |
103 |
|
|
a.sibling = null; |
104 |
|
|
a.parent = null; |
105 |
|
|
a.completeThis(); |
106 |
|
|
if (p == null || p.compareAndSetControlState(0, 1)) |
107 |
|
|
break; |
108 |
|
|
try { |
109 |
|
|
p.onComplete(a, s); |
110 |
jsr166 |
1.2 |
} catch (Throwable rex) { |
111 |
dl |
1.1 |
p.completeExceptionally(rex); |
112 |
|
|
return; |
113 |
|
|
} |
114 |
|
|
a = p; |
115 |
|
|
} |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
public final void completeExceptionally(Throwable ex) { |
119 |
|
|
BinaryAsyncAction a = this; |
120 |
|
|
while (!a.isCompletedAbnormally()) { |
121 |
|
|
a.completeThisExceptionally(ex); |
122 |
|
|
BinaryAsyncAction s = a.sibling; |
123 |
|
|
if (s != null) |
124 |
|
|
s.cancel(false); |
125 |
|
|
if (!a.onException() || (a = a.parent) == null) |
126 |
|
|
break; |
127 |
|
|
} |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
public final BinaryAsyncAction getParent() { |
131 |
|
|
return parent; |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
public BinaryAsyncAction getSibling() { |
135 |
|
|
return sibling; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
public void reinitialize() { |
139 |
|
|
parent = sibling = null; |
140 |
|
|
super.reinitialize(); |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
protected final int getControlState() { |
144 |
|
|
return controlState; |
145 |
|
|
} |
146 |
|
|
|
147 |
jsr166 |
1.2 |
protected final boolean compareAndSetControlState(int expect, |
148 |
dl |
1.1 |
int update) { |
149 |
|
|
return controlStateUpdater.compareAndSet(this, expect, update); |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
protected final void setControlState(int value) { |
153 |
|
|
controlState = value; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
protected final void incrementControlState() { |
157 |
|
|
controlStateUpdater.incrementAndGet(this); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
protected final void decrementControlState() { |
161 |
|
|
controlStateUpdater.decrementAndGet(this); |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
} |
165 |
|
|
|
166 |
jsr166 |
1.6 |
static final class AsyncFib extends BinaryAsyncAction { |
167 |
dl |
1.1 |
int number; |
168 |
jsr166 |
1.2 |
public AsyncFib(int n) { |
169 |
dl |
1.1 |
this.number = n; |
170 |
|
|
} |
171 |
jsr166 |
1.2 |
|
172 |
dl |
1.1 |
public final boolean exec() { |
173 |
|
|
AsyncFib f = this; |
174 |
|
|
int n = f.number; |
175 |
|
|
if (n > 1) { |
176 |
|
|
while (n > 1) { |
177 |
|
|
AsyncFib p = f; |
178 |
|
|
AsyncFib r = new AsyncFib(n - 2); |
179 |
|
|
f = new AsyncFib(--n); |
180 |
|
|
p.linkSubtasks(r, f); |
181 |
|
|
r.fork(); |
182 |
|
|
} |
183 |
|
|
f.number = n; |
184 |
|
|
} |
185 |
|
|
f.complete(); |
186 |
|
|
return false; |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) { |
190 |
|
|
number = ((AsyncFib)x).number + ((AsyncFib)y).number; |
191 |
|
|
} |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
|
195 |
jsr166 |
1.6 |
static final class FailingAsyncFib extends BinaryAsyncAction { |
196 |
dl |
1.1 |
int number; |
197 |
jsr166 |
1.2 |
public FailingAsyncFib(int n) { |
198 |
dl |
1.1 |
this.number = n; |
199 |
|
|
} |
200 |
jsr166 |
1.2 |
|
201 |
dl |
1.1 |
public final boolean exec() { |
202 |
|
|
FailingAsyncFib f = this; |
203 |
|
|
int n = f.number; |
204 |
|
|
if (n > 1) { |
205 |
|
|
while (n > 1) { |
206 |
|
|
FailingAsyncFib p = f; |
207 |
|
|
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
208 |
|
|
f = new FailingAsyncFib(--n); |
209 |
|
|
p.linkSubtasks(r, f); |
210 |
|
|
r.fork(); |
211 |
|
|
} |
212 |
|
|
f.number = n; |
213 |
|
|
} |
214 |
|
|
f.complete(); |
215 |
|
|
return false; |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) { |
219 |
|
|
completeExceptionally(new FJException()); |
220 |
|
|
} |
221 |
|
|
} |
222 |
|
|
|
223 |
jsr166 |
1.2 |
/** |
224 |
dl |
1.1 |
* invoke returns when task completes normally. |
225 |
|
|
* isCompletedAbnormally and isCancelled return false for normally |
226 |
|
|
* completed tasks. getRawResult of a RecursiveAction returns null; |
227 |
|
|
*/ |
228 |
|
|
public void testInvoke() { |
229 |
|
|
RecursiveAction a = new RecursiveAction() { |
230 |
|
|
public void compute() { |
231 |
|
|
AsyncFib f = new AsyncFib(8); |
232 |
|
|
f.invoke(); |
233 |
|
|
threadAssertTrue(f.number == 21); |
234 |
|
|
threadAssertTrue(f.isDone()); |
235 |
|
|
threadAssertFalse(f.isCancelled()); |
236 |
|
|
threadAssertFalse(f.isCompletedAbnormally()); |
237 |
|
|
threadAssertTrue(f.getRawResult() == null); |
238 |
|
|
} |
239 |
|
|
}; |
240 |
|
|
mainPool.invoke(a); |
241 |
|
|
} |
242 |
|
|
|
243 |
jsr166 |
1.2 |
/** |
244 |
dl |
1.1 |
* quietlyInvoke task returns when task completes normally. |
245 |
|
|
* isCompletedAbnormally and isCancelled return false for normally |
246 |
|
|
* completed tasks |
247 |
|
|
*/ |
248 |
|
|
public void testQuietlyInvoke() { |
249 |
|
|
RecursiveAction a = new RecursiveAction() { |
250 |
|
|
public void compute() { |
251 |
|
|
AsyncFib f = new AsyncFib(8); |
252 |
|
|
f.quietlyInvoke(); |
253 |
|
|
threadAssertTrue(f.number == 21); |
254 |
|
|
threadAssertTrue(f.isDone()); |
255 |
|
|
threadAssertFalse(f.isCancelled()); |
256 |
|
|
threadAssertFalse(f.isCompletedAbnormally()); |
257 |
|
|
threadAssertTrue(f.getRawResult() == null); |
258 |
|
|
} |
259 |
|
|
}; |
260 |
|
|
mainPool.invoke(a); |
261 |
|
|
} |
262 |
|
|
|
263 |
jsr166 |
1.2 |
/** |
264 |
dl |
1.1 |
* join of a forked task returns when task completes |
265 |
|
|
*/ |
266 |
|
|
public void testForkJoin() { |
267 |
|
|
RecursiveAction a = new RecursiveAction() { |
268 |
|
|
public void compute() { |
269 |
|
|
AsyncFib f = new AsyncFib(8); |
270 |
|
|
f.fork(); |
271 |
|
|
f.join(); |
272 |
|
|
threadAssertTrue(f.number == 21); |
273 |
|
|
threadAssertTrue(f.isDone()); |
274 |
|
|
threadAssertTrue(f.getRawResult() == null); |
275 |
|
|
} |
276 |
|
|
}; |
277 |
|
|
mainPool.invoke(a); |
278 |
|
|
} |
279 |
|
|
|
280 |
jsr166 |
1.2 |
/** |
281 |
dl |
1.1 |
* get of a forked task returns when task completes |
282 |
|
|
*/ |
283 |
|
|
public void testForkGet() { |
284 |
|
|
RecursiveAction a = new RecursiveAction() { |
285 |
|
|
public void compute() { |
286 |
|
|
try { |
287 |
|
|
AsyncFib f = new AsyncFib(8); |
288 |
|
|
f.fork(); |
289 |
|
|
f.get(); |
290 |
|
|
threadAssertTrue(f.number == 21); |
291 |
|
|
threadAssertTrue(f.isDone()); |
292 |
jsr166 |
1.2 |
} catch (Exception ex) { |
293 |
jsr166 |
1.4 |
unexpectedException(ex); |
294 |
dl |
1.1 |
} |
295 |
|
|
} |
296 |
|
|
}; |
297 |
|
|
mainPool.invoke(a); |
298 |
|
|
} |
299 |
|
|
|
300 |
jsr166 |
1.2 |
/** |
301 |
dl |
1.1 |
* timed get of a forked task returns when task completes |
302 |
|
|
*/ |
303 |
|
|
public void testForkTimedGet() { |
304 |
|
|
RecursiveAction a = new RecursiveAction() { |
305 |
|
|
public void compute() { |
306 |
|
|
try { |
307 |
|
|
AsyncFib f = new AsyncFib(8); |
308 |
|
|
f.fork(); |
309 |
jsr166 |
1.7 |
f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
310 |
dl |
1.1 |
threadAssertTrue(f.number == 21); |
311 |
|
|
threadAssertTrue(f.isDone()); |
312 |
jsr166 |
1.2 |
} catch (Exception ex) { |
313 |
jsr166 |
1.4 |
unexpectedException(ex); |
314 |
dl |
1.1 |
} |
315 |
|
|
} |
316 |
|
|
}; |
317 |
|
|
mainPool.invoke(a); |
318 |
|
|
} |
319 |
|
|
|
320 |
jsr166 |
1.2 |
/** |
321 |
dl |
1.1 |
* timed get with null time unit throws NPE |
322 |
|
|
*/ |
323 |
|
|
public void testForkTimedGetNPE() { |
324 |
|
|
RecursiveAction a = new RecursiveAction() { |
325 |
|
|
public void compute() { |
326 |
|
|
try { |
327 |
|
|
AsyncFib f = new AsyncFib(8); |
328 |
|
|
f.fork(); |
329 |
|
|
f.get(5L, null); |
330 |
jsr166 |
1.4 |
shouldThrow(); |
331 |
jsr166 |
1.2 |
} catch (NullPointerException success) { |
332 |
|
|
} catch (Exception ex) { |
333 |
jsr166 |
1.4 |
unexpectedException(ex); |
334 |
dl |
1.1 |
} |
335 |
|
|
} |
336 |
|
|
}; |
337 |
|
|
mainPool.invoke(a); |
338 |
|
|
} |
339 |
|
|
|
340 |
jsr166 |
1.2 |
/** |
341 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task completes |
342 |
|
|
*/ |
343 |
|
|
public void testForkQuietlyJoin() { |
344 |
|
|
RecursiveAction a = new RecursiveAction() { |
345 |
|
|
public void compute() { |
346 |
|
|
AsyncFib f = new AsyncFib(8); |
347 |
|
|
f.fork(); |
348 |
|
|
f.quietlyJoin(); |
349 |
|
|
threadAssertTrue(f.number == 21); |
350 |
|
|
threadAssertTrue(f.isDone()); |
351 |
|
|
} |
352 |
|
|
}; |
353 |
|
|
mainPool.invoke(a); |
354 |
|
|
} |
355 |
|
|
|
356 |
|
|
|
357 |
jsr166 |
1.2 |
/** |
358 |
dl |
1.1 |
* helpQuiesce returns when tasks are complete. |
359 |
|
|
* getQueuedTaskCount returns 0 when quiescent |
360 |
|
|
*/ |
361 |
|
|
public void testForkHelpQuiesce() { |
362 |
|
|
RecursiveAction a = new RecursiveAction() { |
363 |
|
|
public void compute() { |
364 |
|
|
AsyncFib f = new AsyncFib(8); |
365 |
|
|
f.fork(); |
366 |
|
|
f.helpQuiesce(); |
367 |
|
|
threadAssertTrue(f.number == 21); |
368 |
|
|
threadAssertTrue(f.isDone()); |
369 |
|
|
threadAssertTrue(getQueuedTaskCount() == 0); |
370 |
|
|
} |
371 |
|
|
}; |
372 |
|
|
mainPool.invoke(a); |
373 |
|
|
} |
374 |
|
|
|
375 |
|
|
|
376 |
jsr166 |
1.2 |
/** |
377 |
dl |
1.1 |
* invoke task throws exception when task completes abnormally |
378 |
|
|
*/ |
379 |
|
|
public void testAbnormalInvoke() { |
380 |
|
|
RecursiveAction a = new RecursiveAction() { |
381 |
|
|
public void compute() { |
382 |
|
|
try { |
383 |
|
|
FailingAsyncFib f = new FailingAsyncFib(8); |
384 |
|
|
f.invoke(); |
385 |
|
|
shouldThrow(); |
386 |
jsr166 |
1.2 |
} catch (FJException success) { |
387 |
dl |
1.1 |
} |
388 |
|
|
} |
389 |
|
|
}; |
390 |
|
|
mainPool.invoke(a); |
391 |
|
|
} |
392 |
|
|
|
393 |
jsr166 |
1.2 |
/** |
394 |
jsr166 |
1.3 |
* quietlyInvoke task returns when task completes abnormally |
395 |
dl |
1.1 |
*/ |
396 |
|
|
public void testAbnormalQuietlyInvoke() { |
397 |
|
|
RecursiveAction a = new RecursiveAction() { |
398 |
|
|
public void compute() { |
399 |
|
|
FailingAsyncFib f = new FailingAsyncFib(8); |
400 |
|
|
f.quietlyInvoke(); |
401 |
|
|
threadAssertTrue(f.isDone()); |
402 |
|
|
} |
403 |
|
|
}; |
404 |
|
|
mainPool.invoke(a); |
405 |
|
|
} |
406 |
|
|
|
407 |
jsr166 |
1.2 |
/** |
408 |
dl |
1.1 |
* join of a forked task throws exception when task completes abnormally |
409 |
|
|
*/ |
410 |
|
|
public void testAbnormalForkJoin() { |
411 |
|
|
RecursiveAction a = new RecursiveAction() { |
412 |
|
|
public void compute() { |
413 |
|
|
try { |
414 |
|
|
FailingAsyncFib f = new FailingAsyncFib(8); |
415 |
|
|
f.fork(); |
416 |
|
|
f.join(); |
417 |
|
|
shouldThrow(); |
418 |
jsr166 |
1.2 |
} catch (FJException success) { |
419 |
dl |
1.1 |
} |
420 |
|
|
} |
421 |
|
|
}; |
422 |
|
|
mainPool.invoke(a); |
423 |
|
|
} |
424 |
|
|
|
425 |
jsr166 |
1.2 |
/** |
426 |
dl |
1.1 |
* get of a forked task throws exception when task completes abnormally |
427 |
|
|
*/ |
428 |
|
|
public void testAbnormalForkGet() { |
429 |
|
|
RecursiveAction a = new RecursiveAction() { |
430 |
|
|
public void compute() { |
431 |
|
|
try { |
432 |
|
|
FailingAsyncFib f = new FailingAsyncFib(8); |
433 |
|
|
f.fork(); |
434 |
|
|
f.get(); |
435 |
|
|
shouldThrow(); |
436 |
jsr166 |
1.4 |
} catch (ExecutionException success) { |
437 |
|
|
} catch (Exception ex) { |
438 |
|
|
unexpectedException(ex); |
439 |
dl |
1.1 |
} |
440 |
|
|
} |
441 |
|
|
}; |
442 |
|
|
mainPool.invoke(a); |
443 |
|
|
} |
444 |
|
|
|
445 |
jsr166 |
1.2 |
/** |
446 |
dl |
1.1 |
* timed get of a forked task throws exception when task completes abnormally |
447 |
|
|
*/ |
448 |
|
|
public void testAbnormalForkTimedGet() { |
449 |
|
|
RecursiveAction a = new RecursiveAction() { |
450 |
|
|
public void compute() { |
451 |
|
|
try { |
452 |
|
|
FailingAsyncFib f = new FailingAsyncFib(8); |
453 |
|
|
f.fork(); |
454 |
jsr166 |
1.7 |
f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
455 |
dl |
1.1 |
shouldThrow(); |
456 |
jsr166 |
1.4 |
} catch (ExecutionException success) { |
457 |
|
|
} catch (Exception ex) { |
458 |
|
|
unexpectedException(ex); |
459 |
dl |
1.1 |
} |
460 |
|
|
} |
461 |
|
|
}; |
462 |
|
|
mainPool.invoke(a); |
463 |
|
|
} |
464 |
|
|
|
465 |
jsr166 |
1.2 |
/** |
466 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task completes abnormally |
467 |
|
|
*/ |
468 |
|
|
public void testAbnormalForkQuietlyJoin() { |
469 |
|
|
RecursiveAction a = new RecursiveAction() { |
470 |
|
|
public void compute() { |
471 |
|
|
FailingAsyncFib f = new FailingAsyncFib(8); |
472 |
|
|
f.fork(); |
473 |
|
|
f.quietlyJoin(); |
474 |
|
|
threadAssertTrue(f.isDone()); |
475 |
|
|
threadAssertTrue(f.isCompletedAbnormally()); |
476 |
|
|
threadAssertTrue(f.getException() instanceof FJException); |
477 |
|
|
} |
478 |
|
|
}; |
479 |
|
|
mainPool.invoke(a); |
480 |
|
|
} |
481 |
|
|
|
482 |
jsr166 |
1.2 |
/** |
483 |
dl |
1.1 |
* invoke task throws exception when task cancelled |
484 |
|
|
*/ |
485 |
|
|
public void testCancelledInvoke() { |
486 |
|
|
RecursiveAction a = new RecursiveAction() { |
487 |
|
|
public void compute() { |
488 |
|
|
try { |
489 |
|
|
AsyncFib f = new AsyncFib(8); |
490 |
|
|
f.cancel(true); |
491 |
|
|
f.invoke(); |
492 |
|
|
shouldThrow(); |
493 |
jsr166 |
1.2 |
} catch (CancellationException success) { |
494 |
dl |
1.1 |
} |
495 |
|
|
} |
496 |
|
|
}; |
497 |
|
|
mainPool.invoke(a); |
498 |
|
|
} |
499 |
|
|
|
500 |
jsr166 |
1.2 |
/** |
501 |
dl |
1.1 |
* join of a forked task throws exception when task cancelled |
502 |
|
|
*/ |
503 |
|
|
public void testCancelledForkJoin() { |
504 |
|
|
RecursiveAction a = new RecursiveAction() { |
505 |
|
|
public void compute() { |
506 |
|
|
try { |
507 |
|
|
AsyncFib f = new AsyncFib(8); |
508 |
|
|
f.cancel(true); |
509 |
|
|
f.fork(); |
510 |
|
|
f.join(); |
511 |
|
|
shouldThrow(); |
512 |
jsr166 |
1.2 |
} catch (CancellationException success) { |
513 |
dl |
1.1 |
} |
514 |
|
|
} |
515 |
|
|
}; |
516 |
|
|
mainPool.invoke(a); |
517 |
|
|
} |
518 |
|
|
|
519 |
jsr166 |
1.2 |
/** |
520 |
dl |
1.1 |
* get of a forked task throws exception when task cancelled |
521 |
|
|
*/ |
522 |
|
|
public void testCancelledForkGet() { |
523 |
|
|
RecursiveAction a = new RecursiveAction() { |
524 |
|
|
public void compute() { |
525 |
|
|
try { |
526 |
|
|
AsyncFib f = new AsyncFib(8); |
527 |
|
|
f.cancel(true); |
528 |
|
|
f.fork(); |
529 |
|
|
f.get(); |
530 |
|
|
shouldThrow(); |
531 |
jsr166 |
1.4 |
} catch (CancellationException success) { |
532 |
|
|
} catch (Exception ex) { |
533 |
|
|
unexpectedException(ex); |
534 |
dl |
1.1 |
} |
535 |
|
|
} |
536 |
|
|
}; |
537 |
|
|
mainPool.invoke(a); |
538 |
|
|
} |
539 |
|
|
|
540 |
jsr166 |
1.2 |
/** |
541 |
dl |
1.1 |
* timed get of a forked task throws exception when task cancelled |
542 |
|
|
*/ |
543 |
|
|
public void testCancelledForkTimedGet() { |
544 |
|
|
RecursiveAction a = new RecursiveAction() { |
545 |
|
|
public void compute() { |
546 |
|
|
try { |
547 |
|
|
AsyncFib f = new AsyncFib(8); |
548 |
|
|
f.cancel(true); |
549 |
|
|
f.fork(); |
550 |
jsr166 |
1.7 |
f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
551 |
dl |
1.1 |
shouldThrow(); |
552 |
jsr166 |
1.4 |
} catch (CancellationException success) { |
553 |
|
|
} catch (Exception ex) { |
554 |
|
|
unexpectedException(ex); |
555 |
dl |
1.1 |
} |
556 |
|
|
} |
557 |
|
|
}; |
558 |
|
|
mainPool.invoke(a); |
559 |
|
|
} |
560 |
|
|
|
561 |
jsr166 |
1.2 |
/** |
562 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task cancelled |
563 |
|
|
*/ |
564 |
|
|
public void testCancelledForkQuietlyJoin() { |
565 |
|
|
RecursiveAction a = new RecursiveAction() { |
566 |
|
|
public void compute() { |
567 |
|
|
AsyncFib f = new AsyncFib(8); |
568 |
|
|
f.cancel(true); |
569 |
|
|
f.fork(); |
570 |
|
|
f.quietlyJoin(); |
571 |
|
|
threadAssertTrue(f.isDone()); |
572 |
|
|
threadAssertTrue(f.isCompletedAbnormally()); |
573 |
|
|
threadAssertTrue(f.getException() instanceof CancellationException); |
574 |
|
|
} |
575 |
|
|
}; |
576 |
|
|
mainPool.invoke(a); |
577 |
|
|
} |
578 |
|
|
|
579 |
|
|
/** |
580 |
|
|
* getPool of executing task returns its pool |
581 |
|
|
*/ |
582 |
|
|
public void testGetPool() { |
583 |
|
|
RecursiveAction a = new RecursiveAction() { |
584 |
|
|
public void compute() { |
585 |
|
|
threadAssertTrue(getPool() == mainPool); |
586 |
|
|
} |
587 |
|
|
}; |
588 |
|
|
mainPool.invoke(a); |
589 |
|
|
} |
590 |
|
|
|
591 |
|
|
/** |
592 |
|
|
* getPool of non-FJ task returns null |
593 |
|
|
*/ |
594 |
|
|
public void testGetPool2() { |
595 |
|
|
RecursiveAction a = new RecursiveAction() { |
596 |
|
|
public void compute() { |
597 |
|
|
threadAssertTrue(getPool() == null); |
598 |
|
|
} |
599 |
|
|
}; |
600 |
|
|
a.invoke(); |
601 |
|
|
} |
602 |
|
|
|
603 |
|
|
/** |
604 |
|
|
* inForkJoinPool of executing task returns true |
605 |
|
|
*/ |
606 |
|
|
public void testInForkJoinPool() { |
607 |
|
|
RecursiveAction a = new RecursiveAction() { |
608 |
|
|
public void compute() { |
609 |
|
|
threadAssertTrue(inForkJoinPool()); |
610 |
|
|
} |
611 |
|
|
}; |
612 |
|
|
mainPool.invoke(a); |
613 |
|
|
} |
614 |
|
|
|
615 |
|
|
/** |
616 |
|
|
* inForkJoinPool of non-FJ task returns false |
617 |
|
|
*/ |
618 |
|
|
public void testInForkJoinPool2() { |
619 |
|
|
RecursiveAction a = new RecursiveAction() { |
620 |
|
|
public void compute() { |
621 |
|
|
threadAssertTrue(!inForkJoinPool()); |
622 |
|
|
} |
623 |
|
|
}; |
624 |
|
|
a.invoke(); |
625 |
|
|
} |
626 |
|
|
|
627 |
|
|
/** |
628 |
|
|
* setRawResult(null) succeeds |
629 |
|
|
*/ |
630 |
|
|
public void testSetRawResult() { |
631 |
|
|
RecursiveAction a = new RecursiveAction() { |
632 |
|
|
public void compute() { |
633 |
|
|
setRawResult(null); |
634 |
|
|
} |
635 |
|
|
}; |
636 |
|
|
a.invoke(); |
637 |
|
|
} |
638 |
|
|
|
639 |
jsr166 |
1.2 |
/** |
640 |
dl |
1.1 |
* invoke task throws exception after invoking completeExceptionally |
641 |
|
|
*/ |
642 |
|
|
public void testCompleteExceptionally() { |
643 |
|
|
RecursiveAction a = new RecursiveAction() { |
644 |
|
|
public void compute() { |
645 |
|
|
try { |
646 |
|
|
AsyncFib f = new AsyncFib(8); |
647 |
|
|
f.completeExceptionally(new FJException()); |
648 |
|
|
f.invoke(); |
649 |
|
|
shouldThrow(); |
650 |
jsr166 |
1.2 |
} catch (FJException success) { |
651 |
dl |
1.1 |
} |
652 |
|
|
} |
653 |
|
|
}; |
654 |
|
|
mainPool.invoke(a); |
655 |
|
|
} |
656 |
|
|
|
657 |
jsr166 |
1.2 |
/** |
658 |
dl |
1.1 |
* invokeAll(t1, t2) invokes all task arguments |
659 |
|
|
*/ |
660 |
|
|
public void testInvokeAll2() { |
661 |
|
|
RecursiveAction a = new RecursiveAction() { |
662 |
|
|
public void compute() { |
663 |
|
|
AsyncFib f = new AsyncFib(8); |
664 |
|
|
AsyncFib g = new AsyncFib(9); |
665 |
|
|
invokeAll(f, g); |
666 |
|
|
threadAssertTrue(f.isDone()); |
667 |
|
|
threadAssertTrue(f.number == 21); |
668 |
|
|
threadAssertTrue(g.isDone()); |
669 |
|
|
threadAssertTrue(g.number == 34); |
670 |
|
|
} |
671 |
|
|
}; |
672 |
|
|
mainPool.invoke(a); |
673 |
|
|
} |
674 |
|
|
|
675 |
jsr166 |
1.2 |
/** |
676 |
dl |
1.1 |
* invokeAll(tasks) with 1 argument invokes task |
677 |
|
|
*/ |
678 |
|
|
public void testInvokeAll1() { |
679 |
|
|
RecursiveAction a = new RecursiveAction() { |
680 |
|
|
public void compute() { |
681 |
|
|
AsyncFib f = new AsyncFib(8); |
682 |
|
|
invokeAll(f); |
683 |
|
|
threadAssertTrue(f.isDone()); |
684 |
|
|
threadAssertTrue(f.number == 21); |
685 |
|
|
} |
686 |
|
|
}; |
687 |
|
|
mainPool.invoke(a); |
688 |
|
|
} |
689 |
|
|
|
690 |
jsr166 |
1.2 |
/** |
691 |
dl |
1.1 |
* invokeAll(tasks) with > 2 argument invokes tasks |
692 |
|
|
*/ |
693 |
|
|
public void testInvokeAll3() { |
694 |
|
|
RecursiveAction a = new RecursiveAction() { |
695 |
|
|
public void compute() { |
696 |
|
|
AsyncFib f = new AsyncFib(8); |
697 |
|
|
AsyncFib g = new AsyncFib(9); |
698 |
|
|
AsyncFib h = new AsyncFib(7); |
699 |
|
|
invokeAll(f, g, h); |
700 |
|
|
threadAssertTrue(f.isDone()); |
701 |
|
|
threadAssertTrue(f.number == 21); |
702 |
|
|
threadAssertTrue(g.isDone()); |
703 |
|
|
threadAssertTrue(g.number == 34); |
704 |
|
|
threadAssertTrue(h.isDone()); |
705 |
|
|
threadAssertTrue(h.number == 13); |
706 |
|
|
} |
707 |
|
|
}; |
708 |
|
|
mainPool.invoke(a); |
709 |
|
|
} |
710 |
|
|
|
711 |
jsr166 |
1.2 |
/** |
712 |
dl |
1.1 |
* invokeAll(collection) invokes all tasks in the collection |
713 |
|
|
*/ |
714 |
|
|
public void testInvokeAllCollection() { |
715 |
|
|
RecursiveAction a = new RecursiveAction() { |
716 |
|
|
public void compute() { |
717 |
|
|
AsyncFib f = new AsyncFib(8); |
718 |
|
|
AsyncFib g = new AsyncFib(9); |
719 |
|
|
AsyncFib h = new AsyncFib(7); |
720 |
|
|
HashSet set = new HashSet(); |
721 |
|
|
set.add(f); |
722 |
|
|
set.add(g); |
723 |
|
|
set.add(h); |
724 |
|
|
invokeAll(set); |
725 |
|
|
threadAssertTrue(f.isDone()); |
726 |
|
|
threadAssertTrue(f.number == 21); |
727 |
|
|
threadAssertTrue(g.isDone()); |
728 |
|
|
threadAssertTrue(g.number == 34); |
729 |
|
|
threadAssertTrue(h.isDone()); |
730 |
|
|
threadAssertTrue(h.number == 13); |
731 |
|
|
} |
732 |
|
|
}; |
733 |
|
|
mainPool.invoke(a); |
734 |
|
|
} |
735 |
|
|
|
736 |
|
|
|
737 |
jsr166 |
1.2 |
/** |
738 |
dl |
1.1 |
* invokeAll(tasks) with any null task throws NPE |
739 |
|
|
*/ |
740 |
|
|
public void testInvokeAllNPE() { |
741 |
|
|
RecursiveAction a = new RecursiveAction() { |
742 |
|
|
public void compute() { |
743 |
|
|
try { |
744 |
|
|
AsyncFib f = new AsyncFib(8); |
745 |
|
|
AsyncFib g = new AsyncFib(9); |
746 |
|
|
AsyncFib h = null; |
747 |
|
|
invokeAll(f, g, h); |
748 |
|
|
shouldThrow(); |
749 |
|
|
} catch (NullPointerException success) { |
750 |
|
|
} |
751 |
|
|
} |
752 |
|
|
}; |
753 |
|
|
mainPool.invoke(a); |
754 |
|
|
} |
755 |
|
|
|
756 |
jsr166 |
1.2 |
/** |
757 |
dl |
1.1 |
* invokeAll(t1, t2) throw exception if any task does |
758 |
|
|
*/ |
759 |
|
|
public void testAbnormalInvokeAll2() { |
760 |
|
|
RecursiveAction a = new RecursiveAction() { |
761 |
|
|
public void compute() { |
762 |
|
|
try { |
763 |
|
|
AsyncFib f = new AsyncFib(8); |
764 |
|
|
FailingAsyncFib g = new FailingAsyncFib(9); |
765 |
|
|
invokeAll(f, g); |
766 |
|
|
shouldThrow(); |
767 |
jsr166 |
1.2 |
} catch (FJException success) { |
768 |
dl |
1.1 |
} |
769 |
|
|
} |
770 |
|
|
}; |
771 |
|
|
mainPool.invoke(a); |
772 |
|
|
} |
773 |
|
|
|
774 |
jsr166 |
1.2 |
/** |
775 |
dl |
1.1 |
* invokeAll(tasks) with 1 argument throws exception if task does |
776 |
|
|
*/ |
777 |
|
|
public void testAbnormalInvokeAll1() { |
778 |
|
|
RecursiveAction a = new RecursiveAction() { |
779 |
|
|
public void compute() { |
780 |
|
|
try { |
781 |
|
|
FailingAsyncFib g = new FailingAsyncFib(9); |
782 |
|
|
invokeAll(g); |
783 |
|
|
shouldThrow(); |
784 |
jsr166 |
1.2 |
} catch (FJException success) { |
785 |
dl |
1.1 |
} |
786 |
|
|
} |
787 |
|
|
}; |
788 |
|
|
mainPool.invoke(a); |
789 |
|
|
} |
790 |
|
|
|
791 |
jsr166 |
1.2 |
/** |
792 |
dl |
1.1 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
793 |
|
|
*/ |
794 |
|
|
public void testAbnormalInvokeAll3() { |
795 |
|
|
RecursiveAction a = new RecursiveAction() { |
796 |
|
|
public void compute() { |
797 |
|
|
try { |
798 |
|
|
AsyncFib f = new AsyncFib(8); |
799 |
|
|
FailingAsyncFib g = new FailingAsyncFib(9); |
800 |
|
|
AsyncFib h = new AsyncFib(7); |
801 |
|
|
invokeAll(f, g, h); |
802 |
|
|
shouldThrow(); |
803 |
jsr166 |
1.2 |
} catch (FJException success) { |
804 |
dl |
1.1 |
} |
805 |
|
|
} |
806 |
|
|
}; |
807 |
|
|
mainPool.invoke(a); |
808 |
|
|
} |
809 |
|
|
|
810 |
jsr166 |
1.2 |
/** |
811 |
dl |
1.1 |
* invokeAll(collection) throws exception if any task does |
812 |
|
|
*/ |
813 |
|
|
public void testAbnormalInvokeAllCollection() { |
814 |
|
|
RecursiveAction a = new RecursiveAction() { |
815 |
|
|
public void compute() { |
816 |
|
|
try { |
817 |
|
|
FailingAsyncFib f = new FailingAsyncFib(8); |
818 |
|
|
AsyncFib g = new AsyncFib(9); |
819 |
|
|
AsyncFib h = new AsyncFib(7); |
820 |
|
|
HashSet set = new HashSet(); |
821 |
|
|
set.add(f); |
822 |
|
|
set.add(g); |
823 |
|
|
set.add(h); |
824 |
|
|
invokeAll(set); |
825 |
|
|
shouldThrow(); |
826 |
jsr166 |
1.2 |
} catch (FJException success) { |
827 |
dl |
1.1 |
} |
828 |
|
|
} |
829 |
|
|
}; |
830 |
|
|
mainPool.invoke(a); |
831 |
|
|
} |
832 |
|
|
|
833 |
jsr166 |
1.2 |
/** |
834 |
dl |
1.1 |
* tryUnfork returns true for most recent unexecuted task, |
835 |
|
|
* and suppresses execution |
836 |
|
|
*/ |
837 |
|
|
public void testTryUnfork() { |
838 |
|
|
RecursiveAction a = new RecursiveAction() { |
839 |
|
|
public void compute() { |
840 |
|
|
AsyncFib g = new AsyncFib(9); |
841 |
|
|
g.fork(); |
842 |
|
|
AsyncFib f = new AsyncFib(8); |
843 |
|
|
f.fork(); |
844 |
|
|
threadAssertTrue(f.tryUnfork()); |
845 |
|
|
helpQuiesce(); |
846 |
|
|
threadAssertFalse(f.isDone()); |
847 |
|
|
threadAssertTrue(g.isDone()); |
848 |
|
|
} |
849 |
|
|
}; |
850 |
|
|
singletonPool.invoke(a); |
851 |
|
|
} |
852 |
|
|
|
853 |
jsr166 |
1.2 |
/** |
854 |
dl |
1.1 |
* getSurplusQueuedTaskCount returns > 0 when |
855 |
|
|
* there are more tasks than threads |
856 |
|
|
*/ |
857 |
|
|
public void testGetSurplusQueuedTaskCount() { |
858 |
|
|
RecursiveAction a = new RecursiveAction() { |
859 |
|
|
public void compute() { |
860 |
|
|
AsyncFib h = new AsyncFib(7); |
861 |
|
|
h.fork(); |
862 |
|
|
AsyncFib g = new AsyncFib(9); |
863 |
|
|
g.fork(); |
864 |
|
|
AsyncFib f = new AsyncFib(8); |
865 |
|
|
f.fork(); |
866 |
|
|
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
867 |
|
|
helpQuiesce(); |
868 |
|
|
} |
869 |
|
|
}; |
870 |
|
|
singletonPool.invoke(a); |
871 |
|
|
} |
872 |
|
|
|
873 |
jsr166 |
1.2 |
/** |
874 |
dl |
1.1 |
* peekNextLocalTask returns most recent unexecuted task. |
875 |
|
|
*/ |
876 |
|
|
public void testPeekNextLocalTask() { |
877 |
|
|
RecursiveAction a = new RecursiveAction() { |
878 |
|
|
public void compute() { |
879 |
|
|
AsyncFib g = new AsyncFib(9); |
880 |
|
|
g.fork(); |
881 |
|
|
AsyncFib f = new AsyncFib(8); |
882 |
|
|
f.fork(); |
883 |
|
|
threadAssertTrue(peekNextLocalTask() == f); |
884 |
|
|
f.join(); |
885 |
|
|
threadAssertTrue(f.isDone()); |
886 |
|
|
helpQuiesce(); |
887 |
|
|
} |
888 |
|
|
}; |
889 |
|
|
singletonPool.invoke(a); |
890 |
|
|
} |
891 |
|
|
|
892 |
jsr166 |
1.2 |
/** |
893 |
dl |
1.1 |
* pollNextLocalTask returns most recent unexecuted task |
894 |
|
|
* without executing it |
895 |
|
|
*/ |
896 |
|
|
public void testPollNextLocalTask() { |
897 |
|
|
RecursiveAction a = new RecursiveAction() { |
898 |
|
|
public void compute() { |
899 |
|
|
AsyncFib g = new AsyncFib(9); |
900 |
|
|
g.fork(); |
901 |
|
|
AsyncFib f = new AsyncFib(8); |
902 |
|
|
f.fork(); |
903 |
|
|
threadAssertTrue(pollNextLocalTask() == f); |
904 |
|
|
helpQuiesce(); |
905 |
|
|
threadAssertFalse(f.isDone()); |
906 |
|
|
} |
907 |
|
|
}; |
908 |
|
|
singletonPool.invoke(a); |
909 |
|
|
} |
910 |
|
|
|
911 |
jsr166 |
1.2 |
/** |
912 |
dl |
1.1 |
* pollTask returns an unexecuted task |
913 |
|
|
* without executing it |
914 |
|
|
*/ |
915 |
|
|
public void testPollTask() { |
916 |
|
|
RecursiveAction a = new RecursiveAction() { |
917 |
|
|
public void compute() { |
918 |
|
|
AsyncFib g = new AsyncFib(9); |
919 |
|
|
g.fork(); |
920 |
|
|
AsyncFib f = new AsyncFib(8); |
921 |
|
|
f.fork(); |
922 |
|
|
threadAssertTrue(pollTask() == f); |
923 |
|
|
helpQuiesce(); |
924 |
|
|
threadAssertFalse(f.isDone()); |
925 |
|
|
threadAssertTrue(g.isDone()); |
926 |
|
|
} |
927 |
|
|
}; |
928 |
|
|
singletonPool.invoke(a); |
929 |
|
|
} |
930 |
|
|
|
931 |
jsr166 |
1.2 |
/** |
932 |
dl |
1.1 |
* peekNextLocalTask returns least recent unexecuted task in async mode |
933 |
|
|
*/ |
934 |
|
|
public void testPeekNextLocalTaskAsync() { |
935 |
|
|
RecursiveAction a = new RecursiveAction() { |
936 |
|
|
public void compute() { |
937 |
|
|
AsyncFib g = new AsyncFib(9); |
938 |
|
|
g.fork(); |
939 |
|
|
AsyncFib f = new AsyncFib(8); |
940 |
|
|
f.fork(); |
941 |
|
|
threadAssertTrue(peekNextLocalTask() == g); |
942 |
|
|
f.join(); |
943 |
|
|
helpQuiesce(); |
944 |
|
|
threadAssertTrue(f.isDone()); |
945 |
|
|
} |
946 |
|
|
}; |
947 |
|
|
asyncSingletonPool.invoke(a); |
948 |
|
|
} |
949 |
|
|
|
950 |
jsr166 |
1.2 |
/** |
951 |
dl |
1.1 |
* pollNextLocalTask returns least recent unexecuted task |
952 |
|
|
* without executing it, in async mode |
953 |
|
|
*/ |
954 |
|
|
public void testPollNextLocalTaskAsync() { |
955 |
|
|
RecursiveAction a = new RecursiveAction() { |
956 |
|
|
public void compute() { |
957 |
|
|
AsyncFib g = new AsyncFib(9); |
958 |
|
|
g.fork(); |
959 |
|
|
AsyncFib f = new AsyncFib(8); |
960 |
|
|
f.fork(); |
961 |
|
|
threadAssertTrue(pollNextLocalTask() == g); |
962 |
|
|
helpQuiesce(); |
963 |
|
|
threadAssertTrue(f.isDone()); |
964 |
|
|
threadAssertFalse(g.isDone()); |
965 |
|
|
} |
966 |
|
|
}; |
967 |
|
|
asyncSingletonPool.invoke(a); |
968 |
|
|
} |
969 |
|
|
|
970 |
jsr166 |
1.2 |
/** |
971 |
dl |
1.1 |
* pollTask returns an unexecuted task |
972 |
|
|
* without executing it, in async mode |
973 |
|
|
*/ |
974 |
|
|
public void testPollTaskAsync() { |
975 |
|
|
RecursiveAction a = new RecursiveAction() { |
976 |
|
|
public void compute() { |
977 |
|
|
AsyncFib g = new AsyncFib(9); |
978 |
|
|
g.fork(); |
979 |
|
|
AsyncFib f = new AsyncFib(8); |
980 |
|
|
f.fork(); |
981 |
|
|
threadAssertTrue(pollTask() == g); |
982 |
|
|
helpQuiesce(); |
983 |
|
|
threadAssertTrue(f.isDone()); |
984 |
|
|
threadAssertFalse(g.isDone()); |
985 |
|
|
} |
986 |
|
|
}; |
987 |
|
|
asyncSingletonPool.invoke(a); |
988 |
|
|
} |
989 |
|
|
} |