3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
– |
import junit.framework.*; |
7 |
– |
import java.util.concurrent.*; |
8 |
– |
import java.util.*; |
6 |
|
|
7 |
+ |
import junit.framework.*; |
8 |
+ |
import java.util.concurrent.CancellationException; |
9 |
+ |
import java.util.concurrent.SynchronousQueue; |
10 |
+ |
import java.util.concurrent.ExecutionException; |
11 |
+ |
import java.util.concurrent.ForkJoinPool; |
12 |
+ |
import java.util.concurrent.ForkJoinTask; |
13 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
14 |
+ |
import java.util.concurrent.RecursiveAction; |
15 |
+ |
import java.util.concurrent.TimeUnit; |
16 |
+ |
import java.util.concurrent.TimeoutException; |
17 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
18 |
+ |
import java.util.HashSet; |
19 |
|
|
20 |
|
public class RecursiveActionTest extends JSR166TestCase { |
21 |
|
|
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run (suite()); |
23 |
> |
junit.textui.TestRunner.run(suite()); |
24 |
|
} |
25 |
+ |
|
26 |
|
public static Test suite() { |
27 |
< |
return new TestSuite(RecursiveActionTest.class); |
27 |
> |
return new TestSuite(RecursiveActionTest.class); |
28 |
> |
} |
29 |
> |
|
30 |
> |
private static ForkJoinPool mainPool() { |
31 |
> |
return new ForkJoinPool(); |
32 |
> |
} |
33 |
> |
|
34 |
> |
private static ForkJoinPool singletonPool() { |
35 |
> |
return new ForkJoinPool(1); |
36 |
|
} |
37 |
|
|
38 |
< |
static final ForkJoinPool mainPool = new ForkJoinPool(); |
39 |
< |
static final ForkJoinPool singletonPool = new ForkJoinPool(1); |
40 |
< |
static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1); |
41 |
< |
static { |
42 |
< |
asyncSingletonPool.setAsyncMode(true); |
38 |
> |
private static ForkJoinPool asyncSingletonPool() { |
39 |
> |
return new ForkJoinPool(1, |
40 |
> |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
41 |
> |
null, true); |
42 |
> |
} |
43 |
> |
|
44 |
> |
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
45 |
> |
try { |
46 |
> |
checkNotDone(a); |
47 |
> |
|
48 |
> |
assertNull(pool.invoke(a)); |
49 |
> |
|
50 |
> |
checkCompletedNormally(a); |
51 |
> |
} finally { |
52 |
> |
joinPool(pool); |
53 |
> |
} |
54 |
> |
} |
55 |
> |
|
56 |
> |
void checkNotDone(RecursiveAction a) { |
57 |
> |
assertFalse(a.isDone()); |
58 |
> |
assertFalse(a.isCompletedNormally()); |
59 |
> |
assertFalse(a.isCompletedAbnormally()); |
60 |
> |
assertFalse(a.isCancelled()); |
61 |
> |
assertNull(a.getException()); |
62 |
> |
assertNull(a.getRawResult()); |
63 |
> |
|
64 |
> |
if (! ForkJoinTask.inForkJoinPool()) { |
65 |
> |
Thread.currentThread().interrupt(); |
66 |
> |
try { |
67 |
> |
a.get(); |
68 |
> |
shouldThrow(); |
69 |
> |
} catch (InterruptedException success) { |
70 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
71 |
> |
|
72 |
> |
Thread.currentThread().interrupt(); |
73 |
> |
try { |
74 |
> |
a.get(5L, SECONDS); |
75 |
> |
shouldThrow(); |
76 |
> |
} catch (InterruptedException success) { |
77 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
78 |
> |
} |
79 |
> |
|
80 |
> |
try { |
81 |
> |
a.get(0L, SECONDS); |
82 |
> |
shouldThrow(); |
83 |
> |
} catch (TimeoutException success) { |
84 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
85 |
> |
} |
86 |
> |
|
87 |
> |
void checkCompletedNormally(RecursiveAction a) { |
88 |
> |
assertTrue(a.isDone()); |
89 |
> |
assertFalse(a.isCancelled()); |
90 |
> |
assertTrue(a.isCompletedNormally()); |
91 |
> |
assertFalse(a.isCompletedAbnormally()); |
92 |
> |
assertNull(a.getException()); |
93 |
> |
assertNull(a.getRawResult()); |
94 |
> |
assertNull(a.join()); |
95 |
> |
assertFalse(a.cancel(false)); |
96 |
> |
assertFalse(a.cancel(true)); |
97 |
> |
try { |
98 |
> |
assertNull(a.get()); |
99 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
100 |
> |
try { |
101 |
> |
assertNull(a.get(5L, SECONDS)); |
102 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
103 |
> |
} |
104 |
> |
|
105 |
> |
void checkCancelled(RecursiveAction a) { |
106 |
> |
assertTrue(a.isDone()); |
107 |
> |
assertTrue(a.isCancelled()); |
108 |
> |
assertFalse(a.isCompletedNormally()); |
109 |
> |
assertTrue(a.isCompletedAbnormally()); |
110 |
> |
assertTrue(a.getException() instanceof CancellationException); |
111 |
> |
assertNull(a.getRawResult()); |
112 |
> |
|
113 |
> |
try { |
114 |
> |
a.join(); |
115 |
> |
shouldThrow(); |
116 |
> |
} catch (CancellationException success) { |
117 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
118 |
> |
|
119 |
> |
try { |
120 |
> |
a.get(); |
121 |
> |
shouldThrow(); |
122 |
> |
} catch (CancellationException success) { |
123 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
124 |
> |
|
125 |
> |
try { |
126 |
> |
a.get(5L, SECONDS); |
127 |
> |
shouldThrow(); |
128 |
> |
} catch (CancellationException success) { |
129 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
130 |
> |
} |
131 |
> |
|
132 |
> |
void checkCompletedAbnormally(RecursiveAction a, Throwable t) { |
133 |
> |
assertTrue(a.isDone()); |
134 |
> |
assertFalse(a.isCancelled()); |
135 |
> |
assertFalse(a.isCompletedNormally()); |
136 |
> |
assertTrue(a.isCompletedAbnormally()); |
137 |
> |
assertSame(t, a.getException()); |
138 |
> |
assertNull(a.getRawResult()); |
139 |
> |
assertFalse(a.cancel(false)); |
140 |
> |
assertFalse(a.cancel(true)); |
141 |
> |
|
142 |
> |
try { |
143 |
> |
a.join(); |
144 |
> |
shouldThrow(); |
145 |
> |
} catch (Throwable expected) { |
146 |
> |
assertSame(t, expected); |
147 |
> |
} |
148 |
> |
|
149 |
> |
try { |
150 |
> |
a.get(); |
151 |
> |
shouldThrow(); |
152 |
> |
} catch (ExecutionException success) { |
153 |
> |
assertSame(t, success.getCause()); |
154 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
155 |
> |
|
156 |
> |
try { |
157 |
> |
a.get(5L, SECONDS); |
158 |
> |
shouldThrow(); |
159 |
> |
} catch (ExecutionException success) { |
160 |
> |
assertSame(t, success.getCause()); |
161 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
162 |
|
} |
163 |
|
|
164 |
|
static final class FJException extends RuntimeException { |
166 |
|
} |
167 |
|
|
168 |
|
// A simple recursive action for testing |
169 |
< |
static final class FibAction extends RecursiveAction { |
169 |
> |
final class FibAction extends CheckedRecursiveAction { |
170 |
|
final int number; |
171 |
|
int result; |
172 |
|
FibAction(int n) { number = n; } |
173 |
< |
public void compute() { |
173 |
> |
public void realCompute() { |
174 |
|
int n = number; |
175 |
|
if (n <= 1) |
176 |
|
result = n; |
205 |
|
* invoke returns when task completes normally. |
206 |
|
* isCompletedAbnormally and isCancelled return false for normally |
207 |
|
* completed tasks. getRawResult of a RecursiveAction returns null; |
71 |
– |
* |
208 |
|
*/ |
209 |
|
public void testInvoke() { |
210 |
< |
RecursiveAction a = new RecursiveAction() { |
211 |
< |
public void compute() { |
212 |
< |
FibAction f = new FibAction(8); |
213 |
< |
f.invoke(); |
214 |
< |
threadAssertTrue(f.result == 21); |
215 |
< |
threadAssertTrue(f.isDone()); |
216 |
< |
threadAssertFalse(f.isCancelled()); |
217 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
82 |
< |
threadAssertTrue(f.getRawResult() == null); |
83 |
< |
} |
84 |
< |
}; |
85 |
< |
mainPool.invoke(a); |
210 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
211 |
> |
public void realCompute() { |
212 |
> |
FibAction f = new FibAction(8); |
213 |
> |
assertNull(f.invoke()); |
214 |
> |
assertEquals(21, f.result); |
215 |
> |
checkCompletedNormally(f); |
216 |
> |
}}; |
217 |
> |
testInvokeOnPool(mainPool(), a); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
223 |
|
* completed tasks |
224 |
|
*/ |
225 |
|
public void testQuietlyInvoke() { |
226 |
< |
RecursiveAction a = new RecursiveAction() { |
227 |
< |
public void compute() { |
228 |
< |
FibAction f = new FibAction(8); |
229 |
< |
f.quietlyInvoke(); |
230 |
< |
threadAssertTrue(f.result == 21); |
231 |
< |
threadAssertTrue(f.isDone()); |
232 |
< |
threadAssertFalse(f.isCancelled()); |
233 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
102 |
< |
threadAssertTrue(f.getRawResult() == null); |
103 |
< |
} |
104 |
< |
}; |
105 |
< |
mainPool.invoke(a); |
226 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
227 |
> |
public void realCompute() { |
228 |
> |
FibAction f = new FibAction(8); |
229 |
> |
f.quietlyInvoke(); |
230 |
> |
assertEquals(21, f.result); |
231 |
> |
checkCompletedNormally(f); |
232 |
> |
}}; |
233 |
> |
testInvokeOnPool(mainPool(), a); |
234 |
|
} |
235 |
|
|
236 |
|
/** |
237 |
|
* join of a forked task returns when task completes |
238 |
|
*/ |
239 |
|
public void testForkJoin() { |
240 |
< |
RecursiveAction a = new RecursiveAction() { |
241 |
< |
public void compute() { |
242 |
< |
FibAction f = new FibAction(8); |
243 |
< |
f.fork(); |
240 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
241 |
> |
public void realCompute() { |
242 |
> |
FibAction f = new FibAction(8); |
243 |
> |
assertSame(f, f.fork()); |
244 |
> |
assertNull(f.join()); |
245 |
> |
assertEquals(21, f.result); |
246 |
> |
checkCompletedNormally(f); |
247 |
> |
}}; |
248 |
> |
testInvokeOnPool(mainPool(), a); |
249 |
> |
} |
250 |
> |
|
251 |
> |
/** |
252 |
> |
* join/quietlyJoin of a forked task succeeds in the presence of interrupts |
253 |
> |
*/ |
254 |
> |
public void testJoinIgnoresInterrupts() { |
255 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
256 |
> |
public void realCompute() { |
257 |
> |
FibAction f = new FibAction(8); |
258 |
> |
|
259 |
> |
// test join() |
260 |
> |
assertSame(f, f.fork()); |
261 |
> |
Thread.currentThread().interrupt(); |
262 |
> |
assertNull(f.join()); |
263 |
> |
Thread.interrupted(); |
264 |
> |
assertEquals(21, f.result); |
265 |
> |
checkCompletedNormally(f); |
266 |
> |
|
267 |
> |
f.reinitialize(); |
268 |
> |
f.cancel(true); |
269 |
> |
assertSame(f, f.fork()); |
270 |
> |
Thread.currentThread().interrupt(); |
271 |
> |
try { |
272 |
|
f.join(); |
273 |
< |
threadAssertTrue(f.result == 21); |
274 |
< |
threadAssertTrue(f.isDone()); |
275 |
< |
threadAssertTrue(f.getRawResult() == null); |
273 |
> |
shouldThrow(); |
274 |
> |
} catch (CancellationException success) { |
275 |
> |
Thread.interrupted(); |
276 |
> |
checkCancelled(f); |
277 |
|
} |
278 |
< |
}; |
279 |
< |
mainPool.invoke(a); |
278 |
> |
|
279 |
> |
f.reinitialize(); |
280 |
> |
f.completeExceptionally(new FJException()); |
281 |
> |
assertSame(f, f.fork()); |
282 |
> |
Thread.currentThread().interrupt(); |
283 |
> |
try { |
284 |
> |
f.join(); |
285 |
> |
shouldThrow(); |
286 |
> |
} catch (FJException success) { |
287 |
> |
Thread.interrupted(); |
288 |
> |
checkCompletedAbnormally(f, success); |
289 |
> |
} |
290 |
> |
|
291 |
> |
// test quietlyJoin() |
292 |
> |
f.reinitialize(); |
293 |
> |
assertSame(f, f.fork()); |
294 |
> |
Thread.currentThread().interrupt(); |
295 |
> |
f.quietlyJoin(); |
296 |
> |
Thread.interrupted(); |
297 |
> |
assertEquals(21, f.result); |
298 |
> |
checkCompletedNormally(f); |
299 |
> |
|
300 |
> |
f.reinitialize(); |
301 |
> |
f.cancel(true); |
302 |
> |
assertSame(f, f.fork()); |
303 |
> |
Thread.currentThread().interrupt(); |
304 |
> |
f.quietlyJoin(); |
305 |
> |
Thread.interrupted(); |
306 |
> |
checkCancelled(f); |
307 |
> |
|
308 |
> |
f.reinitialize(); |
309 |
> |
f.completeExceptionally(new FJException()); |
310 |
> |
assertSame(f, f.fork()); |
311 |
> |
Thread.currentThread().interrupt(); |
312 |
> |
f.quietlyJoin(); |
313 |
> |
Thread.interrupted(); |
314 |
> |
checkCompletedAbnormally(f, f.getException()); |
315 |
> |
}}; |
316 |
> |
testInvokeOnPool(mainPool(), a); |
317 |
> |
a.reinitialize(); |
318 |
> |
testInvokeOnPool(singletonPool(), a); |
319 |
> |
} |
320 |
> |
|
321 |
> |
/** |
322 |
> |
* join/quietlyJoin of a forked task when not in ForkJoinPool |
323 |
> |
* succeeds in the presence of interrupts |
324 |
> |
*/ |
325 |
> |
public void testJoinIgnoresInterruptsOutsideForkJoinPool() { |
326 |
> |
final SynchronousQueue<FibAction[]> sq = |
327 |
> |
new SynchronousQueue<FibAction[]>(); |
328 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
329 |
> |
public void realCompute() throws InterruptedException { |
330 |
> |
FibAction[] fibActions = new FibAction[6]; |
331 |
> |
for (int i = 0; i < fibActions.length; i++) |
332 |
> |
fibActions[i] = new FibAction(8); |
333 |
> |
|
334 |
> |
fibActions[1].cancel(false); |
335 |
> |
fibActions[2].completeExceptionally(new FJException()); |
336 |
> |
fibActions[4].cancel(true); |
337 |
> |
fibActions[5].completeExceptionally(new FJException()); |
338 |
> |
|
339 |
> |
for (int i = 0; i < fibActions.length; i++) |
340 |
> |
fibActions[i].fork(); |
341 |
> |
|
342 |
> |
sq.put(fibActions); |
343 |
> |
|
344 |
> |
helpQuiesce(); |
345 |
> |
}}; |
346 |
> |
|
347 |
> |
Runnable r = new CheckedRunnable() { |
348 |
> |
public void realRun() throws InterruptedException { |
349 |
> |
FibAction[] fibActions = sq.take(); |
350 |
> |
FibAction f; |
351 |
> |
|
352 |
> |
// test join() ------------ |
353 |
> |
|
354 |
> |
f = fibActions[0]; |
355 |
> |
assertFalse(ForkJoinTask.inForkJoinPool()); |
356 |
> |
Thread.currentThread().interrupt(); |
357 |
> |
assertNull(f.join()); |
358 |
> |
assertTrue(Thread.interrupted()); |
359 |
> |
assertEquals(21, f.result); |
360 |
> |
checkCompletedNormally(f); |
361 |
> |
|
362 |
> |
f = fibActions[1]; |
363 |
> |
Thread.currentThread().interrupt(); |
364 |
> |
try { |
365 |
> |
f.join(); |
366 |
> |
shouldThrow(); |
367 |
> |
} catch (CancellationException success) { |
368 |
> |
assertTrue(Thread.interrupted()); |
369 |
> |
checkCancelled(f); |
370 |
> |
} |
371 |
> |
|
372 |
> |
f = fibActions[2]; |
373 |
> |
Thread.currentThread().interrupt(); |
374 |
> |
try { |
375 |
> |
f.join(); |
376 |
> |
shouldThrow(); |
377 |
> |
} catch (FJException success) { |
378 |
> |
assertTrue(Thread.interrupted()); |
379 |
> |
checkCompletedAbnormally(f, success); |
380 |
> |
} |
381 |
> |
|
382 |
> |
// test quietlyJoin() --------- |
383 |
> |
|
384 |
> |
f = fibActions[3]; |
385 |
> |
Thread.currentThread().interrupt(); |
386 |
> |
f.quietlyJoin(); |
387 |
> |
assertTrue(Thread.interrupted()); |
388 |
> |
assertEquals(21, f.result); |
389 |
> |
checkCompletedNormally(f); |
390 |
> |
|
391 |
> |
f = fibActions[4]; |
392 |
> |
Thread.currentThread().interrupt(); |
393 |
> |
f.quietlyJoin(); |
394 |
> |
assertTrue(Thread.interrupted()); |
395 |
> |
checkCancelled(f); |
396 |
> |
|
397 |
> |
f = fibActions[5]; |
398 |
> |
Thread.currentThread().interrupt(); |
399 |
> |
f.quietlyJoin(); |
400 |
> |
assertTrue(Thread.interrupted()); |
401 |
> |
assertTrue(f.getException() instanceof FJException); |
402 |
> |
checkCompletedAbnormally(f, f.getException()); |
403 |
> |
}}; |
404 |
> |
|
405 |
> |
Thread t; |
406 |
> |
|
407 |
> |
t = newStartedThread(r); |
408 |
> |
testInvokeOnPool(mainPool(), a); |
409 |
> |
awaitTermination(t, LONG_DELAY_MS); |
410 |
> |
|
411 |
> |
a.reinitialize(); |
412 |
> |
t = newStartedThread(r); |
413 |
> |
testInvokeOnPool(singletonPool(), a); |
414 |
> |
awaitTermination(t, LONG_DELAY_MS); |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
|
* get of a forked task returns when task completes |
419 |
|
*/ |
420 |
|
public void testForkGet() { |
421 |
< |
RecursiveAction a = new RecursiveAction() { |
422 |
< |
public void compute() { |
423 |
< |
try { |
424 |
< |
FibAction f = new FibAction(8); |
425 |
< |
f.fork(); |
426 |
< |
f.get(); |
427 |
< |
threadAssertTrue(f.result == 21); |
428 |
< |
threadAssertTrue(f.isDone()); |
429 |
< |
} catch (Exception ex) { |
138 |
< |
unexpectedException(); |
139 |
< |
} |
140 |
< |
} |
141 |
< |
}; |
142 |
< |
mainPool.invoke(a); |
421 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
422 |
> |
public void realCompute() throws Exception { |
423 |
> |
FibAction f = new FibAction(8); |
424 |
> |
assertSame(f, f.fork()); |
425 |
> |
assertNull(f.get()); |
426 |
> |
assertEquals(21, f.result); |
427 |
> |
checkCompletedNormally(f); |
428 |
> |
}}; |
429 |
> |
testInvokeOnPool(mainPool(), a); |
430 |
|
} |
431 |
|
|
432 |
|
/** |
433 |
|
* timed get of a forked task returns when task completes |
434 |
|
*/ |
435 |
|
public void testForkTimedGet() { |
436 |
< |
RecursiveAction a = new RecursiveAction() { |
437 |
< |
public void compute() { |
438 |
< |
try { |
439 |
< |
FibAction f = new FibAction(8); |
440 |
< |
f.fork(); |
441 |
< |
f.get(5L, TimeUnit.SECONDS); |
442 |
< |
threadAssertTrue(f.result == 21); |
443 |
< |
threadAssertTrue(f.isDone()); |
444 |
< |
} catch (Exception ex) { |
158 |
< |
unexpectedException(); |
159 |
< |
} |
160 |
< |
} |
161 |
< |
}; |
162 |
< |
mainPool.invoke(a); |
436 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
437 |
> |
public void realCompute() throws Exception { |
438 |
> |
FibAction f = new FibAction(8); |
439 |
> |
assertSame(f, f.fork()); |
440 |
> |
assertNull(f.get(5L, SECONDS)); |
441 |
> |
assertEquals(21, f.result); |
442 |
> |
checkCompletedNormally(f); |
443 |
> |
}}; |
444 |
> |
testInvokeOnPool(mainPool(), a); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
|
* timed get with null time unit throws NPE |
449 |
|
*/ |
450 |
|
public void testForkTimedGetNPE() { |
451 |
< |
RecursiveAction a = new RecursiveAction() { |
452 |
< |
public void compute() { |
453 |
< |
try { |
454 |
< |
FibAction f = new FibAction(8); |
455 |
< |
f.fork(); |
456 |
< |
f.get(5L, null); |
457 |
< |
} catch (NullPointerException success) { |
458 |
< |
} catch (Exception ex) { |
459 |
< |
unexpectedException(); |
460 |
< |
} |
179 |
< |
} |
180 |
< |
}; |
181 |
< |
mainPool.invoke(a); |
182 |
< |
} |
183 |
< |
|
184 |
< |
/** |
185 |
< |
* helpJoin of a forked task returns when task completes |
186 |
< |
*/ |
187 |
< |
public void testForkHelpJoin() { |
188 |
< |
RecursiveAction a = new RecursiveAction() { |
189 |
< |
public void compute() { |
190 |
< |
FibAction f = new FibAction(8); |
191 |
< |
f.fork(); |
192 |
< |
f.helpJoin(); |
193 |
< |
threadAssertTrue(f.result == 21); |
194 |
< |
threadAssertTrue(f.isDone()); |
195 |
< |
} |
196 |
< |
}; |
197 |
< |
mainPool.invoke(a); |
451 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
452 |
> |
public void realCompute() throws Exception { |
453 |
> |
FibAction f = new FibAction(8); |
454 |
> |
assertSame(f, f.fork()); |
455 |
> |
try { |
456 |
> |
f.get(5L, null); |
457 |
> |
shouldThrow(); |
458 |
> |
} catch (NullPointerException success) {} |
459 |
> |
}}; |
460 |
> |
testInvokeOnPool(mainPool(), a); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
464 |
|
* quietlyJoin of a forked task returns when task completes |
465 |
|
*/ |
466 |
|
public void testForkQuietlyJoin() { |
467 |
< |
RecursiveAction a = new RecursiveAction() { |
468 |
< |
public void compute() { |
469 |
< |
FibAction f = new FibAction(8); |
470 |
< |
f.fork(); |
471 |
< |
f.quietlyJoin(); |
472 |
< |
threadAssertTrue(f.result == 21); |
473 |
< |
threadAssertTrue(f.isDone()); |
474 |
< |
} |
475 |
< |
}; |
213 |
< |
mainPool.invoke(a); |
214 |
< |
} |
215 |
< |
|
216 |
< |
|
217 |
< |
/** |
218 |
< |
* quietlyHelpJoin of a forked task returns when task completes |
219 |
< |
*/ |
220 |
< |
public void testForkQuietlyHelpJoin() { |
221 |
< |
RecursiveAction a = new RecursiveAction() { |
222 |
< |
public void compute() { |
223 |
< |
FibAction f = new FibAction(8); |
224 |
< |
f.fork(); |
225 |
< |
f.quietlyHelpJoin(); |
226 |
< |
threadAssertTrue(f.result == 21); |
227 |
< |
threadAssertTrue(f.isDone()); |
228 |
< |
} |
229 |
< |
}; |
230 |
< |
mainPool.invoke(a); |
467 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
468 |
> |
public void realCompute() { |
469 |
> |
FibAction f = new FibAction(8); |
470 |
> |
assertSame(f, f.fork()); |
471 |
> |
f.quietlyJoin(); |
472 |
> |
assertEquals(21, f.result); |
473 |
> |
checkCompletedNormally(f); |
474 |
> |
}}; |
475 |
> |
testInvokeOnPool(mainPool(), a); |
476 |
|
} |
477 |
|
|
478 |
|
|
481 |
|
* getQueuedTaskCount returns 0 when quiescent |
482 |
|
*/ |
483 |
|
public void testForkHelpQuiesce() { |
484 |
< |
RecursiveAction a = new RecursiveAction() { |
485 |
< |
public void compute() { |
486 |
< |
FibAction f = new FibAction(8); |
487 |
< |
f.fork(); |
488 |
< |
f.helpQuiesce(); |
489 |
< |
threadAssertTrue(f.result == 21); |
490 |
< |
threadAssertTrue(f.isDone()); |
491 |
< |
threadAssertTrue(getQueuedTaskCount() == 0); |
492 |
< |
} |
493 |
< |
}; |
249 |
< |
mainPool.invoke(a); |
484 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
485 |
> |
public void realCompute() { |
486 |
> |
FibAction f = new FibAction(8); |
487 |
> |
assertSame(f, f.fork()); |
488 |
> |
f.helpQuiesce(); |
489 |
> |
assertEquals(21, f.result); |
490 |
> |
assertEquals(0, getQueuedTaskCount()); |
491 |
> |
checkCompletedNormally(f); |
492 |
> |
}}; |
493 |
> |
testInvokeOnPool(mainPool(), a); |
494 |
|
} |
495 |
|
|
496 |
|
|
498 |
|
* invoke task throws exception when task completes abnormally |
499 |
|
*/ |
500 |
|
public void testAbnormalInvoke() { |
501 |
< |
RecursiveAction a = new RecursiveAction() { |
502 |
< |
public void compute() { |
503 |
< |
try { |
504 |
< |
FailingFibAction f = new FailingFibAction(8); |
505 |
< |
f.invoke(); |
506 |
< |
shouldThrow(); |
507 |
< |
} catch (FJException success) { |
508 |
< |
} |
501 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
502 |
> |
public void realCompute() { |
503 |
> |
FailingFibAction f = new FailingFibAction(8); |
504 |
> |
try { |
505 |
> |
f.invoke(); |
506 |
> |
shouldThrow(); |
507 |
> |
} catch (FJException success) { |
508 |
> |
checkCompletedAbnormally(f, success); |
509 |
|
} |
510 |
< |
}; |
511 |
< |
mainPool.invoke(a); |
510 |
> |
}}; |
511 |
> |
testInvokeOnPool(mainPool(), a); |
512 |
|
} |
513 |
|
|
514 |
|
/** |
515 |
< |
* quietelyInvoke task returns when task completes abnormally |
515 |
> |
* quietlyInvoke task returns when task completes abnormally |
516 |
|
*/ |
517 |
|
public void testAbnormalQuietlyInvoke() { |
518 |
< |
RecursiveAction a = new RecursiveAction() { |
519 |
< |
public void compute() { |
520 |
< |
FailingFibAction f = new FailingFibAction(8); |
521 |
< |
f.quietlyInvoke(); |
522 |
< |
threadAssertTrue(f.isDone()); |
523 |
< |
} |
524 |
< |
}; |
525 |
< |
mainPool.invoke(a); |
518 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
519 |
> |
public void realCompute() { |
520 |
> |
FailingFibAction f = new FailingFibAction(8); |
521 |
> |
f.quietlyInvoke(); |
522 |
> |
assertTrue(f.getException() instanceof FJException); |
523 |
> |
checkCompletedAbnormally(f, f.getException()); |
524 |
> |
}}; |
525 |
> |
testInvokeOnPool(mainPool(), a); |
526 |
|
} |
527 |
|
|
528 |
|
/** |
529 |
|
* join of a forked task throws exception when task completes abnormally |
530 |
|
*/ |
531 |
|
public void testAbnormalForkJoin() { |
532 |
< |
RecursiveAction a = new RecursiveAction() { |
533 |
< |
public void compute() { |
534 |
< |
try { |
535 |
< |
FailingFibAction f = new FailingFibAction(8); |
536 |
< |
f.fork(); |
537 |
< |
f.join(); |
538 |
< |
shouldThrow(); |
539 |
< |
} catch (FJException success) { |
540 |
< |
} |
532 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
533 |
> |
public void realCompute() { |
534 |
> |
FailingFibAction f = new FailingFibAction(8); |
535 |
> |
assertSame(f, f.fork()); |
536 |
> |
try { |
537 |
> |
f.join(); |
538 |
> |
shouldThrow(); |
539 |
> |
} catch (FJException success) { |
540 |
> |
checkCompletedAbnormally(f, success); |
541 |
|
} |
542 |
< |
}; |
543 |
< |
mainPool.invoke(a); |
542 |
> |
}}; |
543 |
> |
testInvokeOnPool(mainPool(), a); |
544 |
|
} |
545 |
|
|
546 |
|
/** |
547 |
|
* get of a forked task throws exception when task completes abnormally |
548 |
|
*/ |
549 |
|
public void testAbnormalForkGet() { |
550 |
< |
RecursiveAction a = new RecursiveAction() { |
551 |
< |
public void compute() { |
552 |
< |
try { |
553 |
< |
FailingFibAction f = new FailingFibAction(8); |
554 |
< |
f.fork(); |
555 |
< |
f.get(); |
556 |
< |
shouldThrow(); |
557 |
< |
} catch (Exception success) { |
558 |
< |
} |
550 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
551 |
> |
public void realCompute() throws Exception { |
552 |
> |
FailingFibAction f = new FailingFibAction(8); |
553 |
> |
assertSame(f, f.fork()); |
554 |
> |
try { |
555 |
> |
f.get(); |
556 |
> |
shouldThrow(); |
557 |
> |
} catch (ExecutionException success) { |
558 |
> |
Throwable cause = success.getCause(); |
559 |
> |
assertTrue(cause instanceof FJException); |
560 |
> |
checkCompletedAbnormally(f, cause); |
561 |
|
} |
562 |
< |
}; |
563 |
< |
mainPool.invoke(a); |
562 |
> |
}}; |
563 |
> |
testInvokeOnPool(mainPool(), a); |
564 |
|
} |
565 |
|
|
566 |
|
/** |
567 |
|
* timed get of a forked task throws exception when task completes abnormally |
568 |
|
*/ |
569 |
|
public void testAbnormalForkTimedGet() { |
570 |
< |
RecursiveAction a = new RecursiveAction() { |
571 |
< |
public void compute() { |
572 |
< |
try { |
573 |
< |
FailingFibAction f = new FailingFibAction(8); |
574 |
< |
f.fork(); |
575 |
< |
f.get(5L, TimeUnit.SECONDS); |
576 |
< |
shouldThrow(); |
577 |
< |
} catch (Exception success) { |
578 |
< |
} |
570 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
571 |
> |
public void realCompute() throws Exception { |
572 |
> |
FailingFibAction f = new FailingFibAction(8); |
573 |
> |
assertSame(f, f.fork()); |
574 |
> |
try { |
575 |
> |
f.get(5L, TimeUnit.SECONDS); |
576 |
> |
shouldThrow(); |
577 |
> |
} catch (ExecutionException success) { |
578 |
> |
Throwable cause = success.getCause(); |
579 |
> |
assertTrue(cause instanceof FJException); |
580 |
> |
checkCompletedAbnormally(f, cause); |
581 |
|
} |
582 |
< |
}; |
583 |
< |
mainPool.invoke(a); |
336 |
< |
} |
337 |
< |
|
338 |
< |
/** |
339 |
< |
* join of a forked task throws exception when task completes abnormally |
340 |
< |
*/ |
341 |
< |
public void testAbnormalForkHelpJoin() { |
342 |
< |
RecursiveAction a = new RecursiveAction() { |
343 |
< |
public void compute() { |
344 |
< |
try { |
345 |
< |
FailingFibAction f = new FailingFibAction(8); |
346 |
< |
f.fork(); |
347 |
< |
f.helpJoin(); |
348 |
< |
shouldThrow(); |
349 |
< |
} catch (FJException success) { |
350 |
< |
} |
351 |
< |
} |
352 |
< |
}; |
353 |
< |
mainPool.invoke(a); |
354 |
< |
} |
355 |
< |
|
356 |
< |
/** |
357 |
< |
* quietlyHelpJoin of a forked task returns when task completes abnormally. |
358 |
< |
* getException of failed task returns its exception. |
359 |
< |
* isCompletedAbnormally of a failed task returns true. |
360 |
< |
* isCancelled of a failed uncancelled task returns false |
361 |
< |
*/ |
362 |
< |
public void testAbnormalForkQuietlyHelpJoin() { |
363 |
< |
RecursiveAction a = new RecursiveAction() { |
364 |
< |
public void compute() { |
365 |
< |
FailingFibAction f = new FailingFibAction(8); |
366 |
< |
f.fork(); |
367 |
< |
f.quietlyHelpJoin(); |
368 |
< |
threadAssertTrue(f.isDone()); |
369 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
370 |
< |
threadAssertFalse(f.isCancelled()); |
371 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
372 |
< |
} |
373 |
< |
}; |
374 |
< |
mainPool.invoke(a); |
582 |
> |
}}; |
583 |
> |
testInvokeOnPool(mainPool(), a); |
584 |
|
} |
585 |
|
|
586 |
|
/** |
587 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
588 |
|
*/ |
589 |
|
public void testAbnormalForkQuietlyJoin() { |
590 |
< |
RecursiveAction a = new RecursiveAction() { |
591 |
< |
public void compute() { |
592 |
< |
FailingFibAction f = new FailingFibAction(8); |
593 |
< |
f.fork(); |
594 |
< |
f.quietlyJoin(); |
595 |
< |
threadAssertTrue(f.isDone()); |
596 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
597 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
598 |
< |
} |
390 |
< |
}; |
391 |
< |
mainPool.invoke(a); |
590 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
591 |
> |
public void realCompute() { |
592 |
> |
FailingFibAction f = new FailingFibAction(8); |
593 |
> |
assertSame(f, f.fork()); |
594 |
> |
f.quietlyJoin(); |
595 |
> |
assertTrue(f.getException() instanceof FJException); |
596 |
> |
checkCompletedAbnormally(f, f.getException()); |
597 |
> |
}}; |
598 |
> |
testInvokeOnPool(mainPool(), a); |
599 |
|
} |
600 |
|
|
601 |
|
/** |
602 |
|
* invoke task throws exception when task cancelled |
603 |
|
*/ |
604 |
|
public void testCancelledInvoke() { |
605 |
< |
RecursiveAction a = new RecursiveAction() { |
606 |
< |
public void compute() { |
607 |
< |
try { |
608 |
< |
FibAction f = new FibAction(8); |
609 |
< |
f.cancel(true); |
610 |
< |
f.invoke(); |
611 |
< |
shouldThrow(); |
612 |
< |
} catch (CancellationException success) { |
613 |
< |
} |
605 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
606 |
> |
public void realCompute() { |
607 |
> |
FibAction f = new FibAction(8); |
608 |
> |
assertTrue(f.cancel(true)); |
609 |
> |
try { |
610 |
> |
f.invoke(); |
611 |
> |
shouldThrow(); |
612 |
> |
} catch (CancellationException success) { |
613 |
> |
checkCancelled(f); |
614 |
|
} |
615 |
< |
}; |
616 |
< |
mainPool.invoke(a); |
615 |
> |
}}; |
616 |
> |
testInvokeOnPool(mainPool(), a); |
617 |
|
} |
618 |
|
|
619 |
|
/** |
620 |
|
* join of a forked task throws exception when task cancelled |
621 |
|
*/ |
622 |
|
public void testCancelledForkJoin() { |
623 |
< |
RecursiveAction a = new RecursiveAction() { |
624 |
< |
public void compute() { |
625 |
< |
try { |
626 |
< |
FibAction f = new FibAction(8); |
627 |
< |
f.cancel(true); |
628 |
< |
f.fork(); |
629 |
< |
f.join(); |
630 |
< |
shouldThrow(); |
631 |
< |
} catch (CancellationException success) { |
632 |
< |
} |
623 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
624 |
> |
public void realCompute() { |
625 |
> |
FibAction f = new FibAction(8); |
626 |
> |
assertTrue(f.cancel(true)); |
627 |
> |
assertSame(f, f.fork()); |
628 |
> |
try { |
629 |
> |
f.join(); |
630 |
> |
shouldThrow(); |
631 |
> |
} catch (CancellationException success) { |
632 |
> |
checkCancelled(f); |
633 |
|
} |
634 |
< |
}; |
635 |
< |
mainPool.invoke(a); |
634 |
> |
}}; |
635 |
> |
testInvokeOnPool(mainPool(), a); |
636 |
|
} |
637 |
|
|
638 |
|
/** |
639 |
|
* get of a forked task throws exception when task cancelled |
640 |
|
*/ |
641 |
|
public void testCancelledForkGet() { |
642 |
< |
RecursiveAction a = new RecursiveAction() { |
643 |
< |
public void compute() { |
644 |
< |
try { |
645 |
< |
FibAction f = new FibAction(8); |
646 |
< |
f.cancel(true); |
647 |
< |
f.fork(); |
648 |
< |
f.get(); |
649 |
< |
shouldThrow(); |
650 |
< |
} catch (Exception success) { |
651 |
< |
} |
642 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
643 |
> |
public void realCompute() throws Exception { |
644 |
> |
FibAction f = new FibAction(8); |
645 |
> |
assertTrue(f.cancel(true)); |
646 |
> |
assertSame(f, f.fork()); |
647 |
> |
try { |
648 |
> |
f.get(); |
649 |
> |
shouldThrow(); |
650 |
> |
} catch (CancellationException success) { |
651 |
> |
checkCancelled(f); |
652 |
|
} |
653 |
< |
}; |
654 |
< |
mainPool.invoke(a); |
653 |
> |
}}; |
654 |
> |
testInvokeOnPool(mainPool(), a); |
655 |
|
} |
656 |
|
|
657 |
|
/** |
658 |
|
* timed get of a forked task throws exception when task cancelled |
659 |
|
*/ |
660 |
|
public void testCancelledForkTimedGet() { |
661 |
< |
RecursiveAction a = new RecursiveAction() { |
662 |
< |
public void compute() { |
663 |
< |
try { |
664 |
< |
FibAction f = new FibAction(8); |
665 |
< |
f.cancel(true); |
666 |
< |
f.fork(); |
667 |
< |
f.get(5L, TimeUnit.SECONDS); |
668 |
< |
shouldThrow(); |
669 |
< |
} catch (Exception success) { |
670 |
< |
} |
464 |
< |
} |
465 |
< |
}; |
466 |
< |
mainPool.invoke(a); |
467 |
< |
} |
468 |
< |
|
469 |
< |
/** |
470 |
< |
* join of a forked task throws exception when task cancelled |
471 |
< |
*/ |
472 |
< |
public void testCancelledForkHelpJoin() { |
473 |
< |
RecursiveAction a = new RecursiveAction() { |
474 |
< |
public void compute() { |
475 |
< |
try { |
476 |
< |
FibAction f = new FibAction(8); |
477 |
< |
f.cancel(true); |
478 |
< |
f.fork(); |
479 |
< |
f.helpJoin(); |
480 |
< |
shouldThrow(); |
481 |
< |
} catch (CancellationException success) { |
482 |
< |
} |
483 |
< |
} |
484 |
< |
}; |
485 |
< |
mainPool.invoke(a); |
486 |
< |
} |
487 |
< |
|
488 |
< |
/** |
489 |
< |
* quietlyHelpJoin of a forked task returns when task cancelled. |
490 |
< |
* getException of cancelled task returns its exception. |
491 |
< |
* isCompletedAbnormally of a cancelled task returns true. |
492 |
< |
* isCancelled of a cancelled task returns true |
493 |
< |
*/ |
494 |
< |
public void testCancelledForkQuietlyHelpJoin() { |
495 |
< |
RecursiveAction a = new RecursiveAction() { |
496 |
< |
public void compute() { |
497 |
< |
FibAction f = new FibAction(8); |
498 |
< |
f.cancel(true); |
499 |
< |
f.fork(); |
500 |
< |
f.quietlyHelpJoin(); |
501 |
< |
threadAssertTrue(f.isDone()); |
502 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
503 |
< |
threadAssertTrue(f.isCancelled()); |
504 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
661 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
662 |
> |
public void realCompute() throws Exception { |
663 |
> |
FibAction f = new FibAction(8); |
664 |
> |
assertTrue(f.cancel(true)); |
665 |
> |
assertSame(f, f.fork()); |
666 |
> |
try { |
667 |
> |
f.get(5L, SECONDS); |
668 |
> |
shouldThrow(); |
669 |
> |
} catch (CancellationException success) { |
670 |
> |
checkCancelled(f); |
671 |
|
} |
672 |
< |
}; |
673 |
< |
mainPool.invoke(a); |
672 |
> |
}}; |
673 |
> |
testInvokeOnPool(mainPool(), a); |
674 |
|
} |
675 |
|
|
676 |
|
/** |
677 |
|
* quietlyJoin of a forked task returns when task cancelled |
678 |
|
*/ |
679 |
|
public void testCancelledForkQuietlyJoin() { |
680 |
< |
RecursiveAction a = new RecursiveAction() { |
681 |
< |
public void compute() { |
682 |
< |
FibAction f = new FibAction(8); |
683 |
< |
f.cancel(true); |
684 |
< |
f.fork(); |
685 |
< |
f.quietlyJoin(); |
686 |
< |
threadAssertTrue(f.isDone()); |
687 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
688 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
523 |
< |
} |
524 |
< |
}; |
525 |
< |
mainPool.invoke(a); |
680 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
681 |
> |
public void realCompute() { |
682 |
> |
FibAction f = new FibAction(8); |
683 |
> |
assertTrue(f.cancel(true)); |
684 |
> |
assertSame(f, f.fork()); |
685 |
> |
f.quietlyJoin(); |
686 |
> |
checkCancelled(f); |
687 |
> |
}}; |
688 |
> |
testInvokeOnPool(mainPool(), a); |
689 |
|
} |
690 |
|
|
691 |
|
/** |
692 |
|
* getPool of executing task returns its pool |
693 |
|
*/ |
694 |
|
public void testGetPool() { |
695 |
< |
RecursiveAction a = new RecursiveAction() { |
696 |
< |
public void compute() { |
697 |
< |
threadAssertTrue(getPool() == mainPool); |
698 |
< |
} |
699 |
< |
}; |
700 |
< |
mainPool.invoke(a); |
695 |
> |
final ForkJoinPool mainPool = mainPool(); |
696 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
697 |
> |
public void realCompute() { |
698 |
> |
assertSame(mainPool, getPool()); |
699 |
> |
}}; |
700 |
> |
testInvokeOnPool(mainPool, a); |
701 |
|
} |
702 |
|
|
703 |
|
/** |
704 |
|
* getPool of non-FJ task returns null |
705 |
|
*/ |
706 |
|
public void testGetPool2() { |
707 |
< |
RecursiveAction a = new RecursiveAction() { |
708 |
< |
public void compute() { |
709 |
< |
threadAssertTrue(getPool() == null); |
710 |
< |
} |
711 |
< |
}; |
549 |
< |
a.invoke(); |
707 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
708 |
> |
public void realCompute() { |
709 |
> |
assertNull(getPool()); |
710 |
> |
}}; |
711 |
> |
assertNull(a.invoke()); |
712 |
|
} |
713 |
|
|
714 |
|
/** |
715 |
|
* inForkJoinPool of executing task returns true |
716 |
|
*/ |
717 |
|
public void testInForkJoinPool() { |
718 |
< |
RecursiveAction a = new RecursiveAction() { |
719 |
< |
public void compute() { |
720 |
< |
threadAssertTrue(inForkJoinPool()); |
721 |
< |
} |
722 |
< |
}; |
561 |
< |
mainPool.invoke(a); |
718 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
719 |
> |
public void realCompute() { |
720 |
> |
assertTrue(inForkJoinPool()); |
721 |
> |
}}; |
722 |
> |
testInvokeOnPool(mainPool(), a); |
723 |
|
} |
724 |
|
|
725 |
|
/** |
726 |
|
* inForkJoinPool of non-FJ task returns false |
727 |
|
*/ |
728 |
|
public void testInForkJoinPool2() { |
729 |
< |
RecursiveAction a = new RecursiveAction() { |
730 |
< |
public void compute() { |
731 |
< |
threadAssertTrue(!inForkJoinPool()); |
732 |
< |
} |
733 |
< |
}; |
573 |
< |
a.invoke(); |
729 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
730 |
> |
public void realCompute() { |
731 |
> |
assertFalse(inForkJoinPool()); |
732 |
> |
}}; |
733 |
> |
assertNull(a.invoke()); |
734 |
|
} |
735 |
|
|
736 |
|
/** |
737 |
|
* getPool of current thread in pool returns its pool |
738 |
|
*/ |
739 |
|
public void testWorkerGetPool() { |
740 |
< |
RecursiveAction a = new RecursiveAction() { |
741 |
< |
public void compute() { |
742 |
< |
ForkJoinWorkerThread w = |
743 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
744 |
< |
threadAssertTrue(w.getPool() == mainPool); |
745 |
< |
} |
746 |
< |
}; |
747 |
< |
mainPool.invoke(a); |
740 |
> |
final ForkJoinPool mainPool = mainPool(); |
741 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
742 |
> |
public void realCompute() { |
743 |
> |
ForkJoinWorkerThread w = |
744 |
> |
(ForkJoinWorkerThread) Thread.currentThread(); |
745 |
> |
assertSame(mainPool, w.getPool()); |
746 |
> |
}}; |
747 |
> |
testInvokeOnPool(mainPool, a); |
748 |
|
} |
749 |
|
|
750 |
|
/** |
751 |
|
* getPoolIndex of current thread in pool returns 0 <= value < poolSize |
592 |
– |
* |
752 |
|
*/ |
753 |
|
public void testWorkerGetPoolIndex() { |
754 |
< |
RecursiveAction a = new RecursiveAction() { |
755 |
< |
public void compute() { |
756 |
< |
ForkJoinWorkerThread w = |
757 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
758 |
< |
int idx = w.getPoolIndex(); |
759 |
< |
threadAssertTrue(idx >= 0); |
760 |
< |
threadAssertTrue(idx < mainPool.getPoolSize()); |
761 |
< |
} |
762 |
< |
}; |
604 |
< |
mainPool.invoke(a); |
754 |
> |
final ForkJoinPool mainPool = mainPool(); |
755 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
756 |
> |
public void realCompute() { |
757 |
> |
ForkJoinWorkerThread w = |
758 |
> |
(ForkJoinWorkerThread)(Thread.currentThread()); |
759 |
> |
assertTrue(w.getPoolIndex() >= 0); |
760 |
> |
assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
761 |
> |
}}; |
762 |
> |
testInvokeOnPool(mainPool, a); |
763 |
|
} |
764 |
|
|
765 |
|
|
767 |
|
* setRawResult(null) succeeds |
768 |
|
*/ |
769 |
|
public void testSetRawResult() { |
770 |
< |
RecursiveAction a = new RecursiveAction() { |
771 |
< |
public void compute() { |
772 |
< |
setRawResult(null); |
773 |
< |
} |
774 |
< |
}; |
775 |
< |
a.invoke(); |
770 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
771 |
> |
public void realCompute() { |
772 |
> |
setRawResult(null); |
773 |
> |
assertNull(getRawResult()); |
774 |
> |
}}; |
775 |
> |
assertNull(a.invoke()); |
776 |
|
} |
777 |
|
|
778 |
|
/** |
779 |
|
* A reinitialized task may be re-invoked |
780 |
|
*/ |
781 |
|
public void testReinitialize() { |
782 |
< |
RecursiveAction a = new RecursiveAction() { |
783 |
< |
public void compute() { |
784 |
< |
FibAction f = new FibAction(8); |
785 |
< |
f.invoke(); |
786 |
< |
threadAssertTrue(f.result == 21); |
787 |
< |
threadAssertTrue(f.isDone()); |
788 |
< |
threadAssertFalse(f.isCancelled()); |
789 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
782 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
783 |
> |
public void realCompute() { |
784 |
> |
FibAction f = new FibAction(8); |
785 |
> |
checkNotDone(f); |
786 |
> |
|
787 |
> |
for (int i = 0; i < 3; i++) { |
788 |
> |
assertNull(f.invoke()); |
789 |
> |
assertEquals(21, f.result); |
790 |
> |
checkCompletedNormally(f); |
791 |
|
f.reinitialize(); |
792 |
< |
f.invoke(); |
634 |
< |
threadAssertTrue(f.result == 21); |
792 |
> |
checkNotDone(f); |
793 |
|
} |
794 |
< |
}; |
795 |
< |
mainPool.invoke(a); |
794 |
> |
}}; |
795 |
> |
testInvokeOnPool(mainPool(), a); |
796 |
|
} |
797 |
|
|
798 |
|
/** |
799 |
|
* invoke task throws exception after invoking completeExceptionally |
800 |
|
*/ |
801 |
|
public void testCompleteExceptionally() { |
802 |
< |
RecursiveAction a = new RecursiveAction() { |
803 |
< |
public void compute() { |
804 |
< |
try { |
805 |
< |
FibAction f = new FibAction(8); |
806 |
< |
f.completeExceptionally(new FJException()); |
807 |
< |
f.invoke(); |
808 |
< |
shouldThrow(); |
809 |
< |
} catch (FJException success) { |
810 |
< |
} |
802 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
803 |
> |
public void realCompute() { |
804 |
> |
FibAction f = new FibAction(8); |
805 |
> |
f.completeExceptionally(new FJException()); |
806 |
> |
try { |
807 |
> |
f.invoke(); |
808 |
> |
shouldThrow(); |
809 |
> |
} catch (FJException success) { |
810 |
> |
checkCompletedAbnormally(f, success); |
811 |
|
} |
812 |
< |
}; |
813 |
< |
mainPool.invoke(a); |
812 |
> |
}}; |
813 |
> |
testInvokeOnPool(mainPool(), a); |
814 |
|
} |
815 |
|
|
816 |
|
/** |
817 |
|
* invoke task suppresses execution invoking complete |
818 |
|
*/ |
819 |
|
public void testComplete() { |
820 |
< |
RecursiveAction a = new RecursiveAction() { |
821 |
< |
public void compute() { |
822 |
< |
FibAction f = new FibAction(8); |
823 |
< |
f.complete(null); |
824 |
< |
f.invoke(); |
825 |
< |
threadAssertTrue(f.isDone()); |
826 |
< |
threadAssertTrue(f.result == 0); |
827 |
< |
} |
828 |
< |
}; |
671 |
< |
mainPool.invoke(a); |
820 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
821 |
> |
public void realCompute() { |
822 |
> |
FibAction f = new FibAction(8); |
823 |
> |
f.complete(null); |
824 |
> |
assertNull(f.invoke()); |
825 |
> |
assertEquals(0, f.result); |
826 |
> |
checkCompletedNormally(f); |
827 |
> |
}}; |
828 |
> |
testInvokeOnPool(mainPool(), a); |
829 |
|
} |
830 |
|
|
831 |
|
/** |
832 |
|
* invokeAll(t1, t2) invokes all task arguments |
833 |
|
*/ |
834 |
|
public void testInvokeAll2() { |
835 |
< |
RecursiveAction a = new RecursiveAction() { |
836 |
< |
public void compute() { |
837 |
< |
FibAction f = new FibAction(8); |
838 |
< |
FibAction g = new FibAction(9); |
839 |
< |
invokeAll(f, g); |
840 |
< |
threadAssertTrue(f.isDone()); |
841 |
< |
threadAssertTrue(f.result == 21); |
842 |
< |
threadAssertTrue(g.isDone()); |
843 |
< |
threadAssertTrue(g.result == 34); |
844 |
< |
} |
845 |
< |
}; |
689 |
< |
mainPool.invoke(a); |
835 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
836 |
> |
public void realCompute() { |
837 |
> |
FibAction f = new FibAction(8); |
838 |
> |
FibAction g = new FibAction(9); |
839 |
> |
invokeAll(f, g); |
840 |
> |
checkCompletedNormally(f); |
841 |
> |
assertEquals(21, f.result); |
842 |
> |
checkCompletedNormally(g); |
843 |
> |
assertEquals(34, g.result); |
844 |
> |
}}; |
845 |
> |
testInvokeOnPool(mainPool(), a); |
846 |
|
} |
847 |
|
|
848 |
|
/** |
849 |
|
* invokeAll(tasks) with 1 argument invokes task |
850 |
|
*/ |
851 |
|
public void testInvokeAll1() { |
852 |
< |
RecursiveAction a = new RecursiveAction() { |
853 |
< |
public void compute() { |
854 |
< |
FibAction f = new FibAction(8); |
855 |
< |
invokeAll(f); |
856 |
< |
threadAssertTrue(f.isDone()); |
857 |
< |
threadAssertTrue(f.result == 21); |
858 |
< |
} |
859 |
< |
}; |
704 |
< |
mainPool.invoke(a); |
852 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
853 |
> |
public void realCompute() { |
854 |
> |
FibAction f = new FibAction(8); |
855 |
> |
invokeAll(f); |
856 |
> |
checkCompletedNormally(f); |
857 |
> |
assertEquals(21, f.result); |
858 |
> |
}}; |
859 |
> |
testInvokeOnPool(mainPool(), a); |
860 |
|
} |
861 |
|
|
862 |
|
/** |
863 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
864 |
|
*/ |
865 |
|
public void testInvokeAll3() { |
866 |
< |
RecursiveAction a = new RecursiveAction() { |
867 |
< |
public void compute() { |
868 |
< |
FibAction f = new FibAction(8); |
869 |
< |
FibAction g = new FibAction(9); |
870 |
< |
FibAction h = new FibAction(7); |
871 |
< |
invokeAll(f, g, h); |
872 |
< |
threadAssertTrue(f.isDone()); |
873 |
< |
threadAssertTrue(f.result == 21); |
874 |
< |
threadAssertTrue(g.isDone()); |
875 |
< |
threadAssertTrue(g.result == 34); |
876 |
< |
threadAssertTrue(h.isDone()); |
877 |
< |
threadAssertTrue(h.result == 13); |
878 |
< |
} |
879 |
< |
}; |
880 |
< |
mainPool.invoke(a); |
866 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
867 |
> |
public void realCompute() { |
868 |
> |
FibAction f = new FibAction(8); |
869 |
> |
FibAction g = new FibAction(9); |
870 |
> |
FibAction h = new FibAction(7); |
871 |
> |
invokeAll(f, g, h); |
872 |
> |
assertTrue(f.isDone()); |
873 |
> |
assertTrue(g.isDone()); |
874 |
> |
assertTrue(h.isDone()); |
875 |
> |
checkCompletedNormally(f); |
876 |
> |
assertEquals(21, f.result); |
877 |
> |
checkCompletedNormally(g); |
878 |
> |
assertEquals(34, g.result); |
879 |
> |
checkCompletedNormally(g); |
880 |
> |
assertEquals(13, h.result); |
881 |
> |
}}; |
882 |
> |
testInvokeOnPool(mainPool(), a); |
883 |
|
} |
884 |
|
|
885 |
|
/** |
886 |
|
* invokeAll(collection) invokes all tasks in the collection |
887 |
|
*/ |
888 |
|
public void testInvokeAllCollection() { |
889 |
< |
RecursiveAction a = new RecursiveAction() { |
890 |
< |
public void compute() { |
891 |
< |
FibAction f = new FibAction(8); |
892 |
< |
FibAction g = new FibAction(9); |
893 |
< |
FibAction h = new FibAction(7); |
894 |
< |
HashSet set = new HashSet(); |
895 |
< |
set.add(f); |
896 |
< |
set.add(g); |
897 |
< |
set.add(h); |
898 |
< |
invokeAll(set); |
899 |
< |
threadAssertTrue(f.isDone()); |
900 |
< |
threadAssertTrue(f.result == 21); |
901 |
< |
threadAssertTrue(g.isDone()); |
902 |
< |
threadAssertTrue(g.result == 34); |
903 |
< |
threadAssertTrue(h.isDone()); |
904 |
< |
threadAssertTrue(h.result == 13); |
905 |
< |
} |
906 |
< |
}; |
907 |
< |
mainPool.invoke(a); |
889 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
890 |
> |
public void realCompute() { |
891 |
> |
FibAction f = new FibAction(8); |
892 |
> |
FibAction g = new FibAction(9); |
893 |
> |
FibAction h = new FibAction(7); |
894 |
> |
HashSet set = new HashSet(); |
895 |
> |
set.add(f); |
896 |
> |
set.add(g); |
897 |
> |
set.add(h); |
898 |
> |
invokeAll(set); |
899 |
> |
assertTrue(f.isDone()); |
900 |
> |
assertTrue(g.isDone()); |
901 |
> |
assertTrue(h.isDone()); |
902 |
> |
checkCompletedNormally(f); |
903 |
> |
assertEquals(21, f.result); |
904 |
> |
checkCompletedNormally(g); |
905 |
> |
assertEquals(34, g.result); |
906 |
> |
checkCompletedNormally(g); |
907 |
> |
assertEquals(13, h.result); |
908 |
> |
}}; |
909 |
> |
testInvokeOnPool(mainPool(), a); |
910 |
|
} |
911 |
|
|
912 |
|
|
914 |
|
* invokeAll(tasks) with any null task throws NPE |
915 |
|
*/ |
916 |
|
public void testInvokeAllNPE() { |
917 |
< |
RecursiveAction a = new RecursiveAction() { |
918 |
< |
public void compute() { |
919 |
< |
try { |
920 |
< |
FibAction f = new FibAction(8); |
921 |
< |
FibAction g = new FibAction(9); |
922 |
< |
FibAction h = null; |
923 |
< |
invokeAll(f, g, h); |
924 |
< |
shouldThrow(); |
925 |
< |
} catch (NullPointerException success) { |
926 |
< |
} |
927 |
< |
} |
769 |
< |
}; |
770 |
< |
mainPool.invoke(a); |
917 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
918 |
> |
public void realCompute() { |
919 |
> |
FibAction f = new FibAction(8); |
920 |
> |
FibAction g = new FibAction(9); |
921 |
> |
FibAction h = null; |
922 |
> |
try { |
923 |
> |
invokeAll(f, g, h); |
924 |
> |
shouldThrow(); |
925 |
> |
} catch (NullPointerException success) {} |
926 |
> |
}}; |
927 |
> |
testInvokeOnPool(mainPool(), a); |
928 |
|
} |
929 |
|
|
930 |
|
/** |
931 |
|
* invokeAll(t1, t2) throw exception if any task does |
932 |
|
*/ |
933 |
|
public void testAbnormalInvokeAll2() { |
934 |
< |
RecursiveAction a = new RecursiveAction() { |
935 |
< |
public void compute() { |
936 |
< |
try { |
937 |
< |
FibAction f = new FibAction(8); |
938 |
< |
FailingFibAction g = new FailingFibAction(9); |
939 |
< |
invokeAll(f, g); |
940 |
< |
shouldThrow(); |
941 |
< |
} catch (FJException success) { |
942 |
< |
} |
934 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
935 |
> |
public void realCompute() { |
936 |
> |
FibAction f = new FibAction(8); |
937 |
> |
FailingFibAction g = new FailingFibAction(9); |
938 |
> |
try { |
939 |
> |
invokeAll(f, g); |
940 |
> |
shouldThrow(); |
941 |
> |
} catch (FJException success) { |
942 |
> |
checkCompletedAbnormally(g, success); |
943 |
|
} |
944 |
< |
}; |
945 |
< |
mainPool.invoke(a); |
944 |
> |
}}; |
945 |
> |
testInvokeOnPool(mainPool(), a); |
946 |
|
} |
947 |
|
|
948 |
|
/** |
949 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
950 |
|
*/ |
951 |
|
public void testAbnormalInvokeAll1() { |
952 |
< |
RecursiveAction a = new RecursiveAction() { |
953 |
< |
public void compute() { |
954 |
< |
try { |
955 |
< |
FailingFibAction g = new FailingFibAction(9); |
956 |
< |
invokeAll(g); |
957 |
< |
shouldThrow(); |
958 |
< |
} catch (FJException success) { |
959 |
< |
} |
952 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
953 |
> |
public void realCompute() { |
954 |
> |
FailingFibAction g = new FailingFibAction(9); |
955 |
> |
try { |
956 |
> |
invokeAll(g); |
957 |
> |
shouldThrow(); |
958 |
> |
} catch (FJException success) { |
959 |
> |
checkCompletedAbnormally(g, success); |
960 |
|
} |
961 |
< |
}; |
962 |
< |
mainPool.invoke(a); |
961 |
> |
}}; |
962 |
> |
testInvokeOnPool(mainPool(), a); |
963 |
|
} |
964 |
|
|
965 |
|
/** |
966 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
967 |
|
*/ |
968 |
|
public void testAbnormalInvokeAll3() { |
969 |
< |
RecursiveAction a = new RecursiveAction() { |
970 |
< |
public void compute() { |
971 |
< |
try { |
972 |
< |
FibAction f = new FibAction(8); |
973 |
< |
FailingFibAction g = new FailingFibAction(9); |
974 |
< |
FibAction h = new FibAction(7); |
975 |
< |
invokeAll(f, g, h); |
976 |
< |
shouldThrow(); |
977 |
< |
} catch (FJException success) { |
978 |
< |
} |
969 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
970 |
> |
public void realCompute() { |
971 |
> |
FibAction f = new FibAction(8); |
972 |
> |
FailingFibAction g = new FailingFibAction(9); |
973 |
> |
FibAction h = new FibAction(7); |
974 |
> |
try { |
975 |
> |
invokeAll(f, g, h); |
976 |
> |
shouldThrow(); |
977 |
> |
} catch (FJException success) { |
978 |
> |
checkCompletedAbnormally(g, success); |
979 |
|
} |
980 |
< |
}; |
981 |
< |
mainPool.invoke(a); |
980 |
> |
}}; |
981 |
> |
testInvokeOnPool(mainPool(), a); |
982 |
|
} |
983 |
|
|
984 |
|
/** |
985 |
|
* invokeAll(collection) throws exception if any task does |
986 |
|
*/ |
987 |
|
public void testAbnormalInvokeAllCollection() { |
988 |
< |
RecursiveAction a = new RecursiveAction() { |
989 |
< |
public void compute() { |
990 |
< |
try { |
991 |
< |
FailingFibAction f = new FailingFibAction(8); |
992 |
< |
FibAction g = new FibAction(9); |
993 |
< |
FibAction h = new FibAction(7); |
994 |
< |
HashSet set = new HashSet(); |
995 |
< |
set.add(f); |
996 |
< |
set.add(g); |
997 |
< |
set.add(h); |
998 |
< |
invokeAll(set); |
999 |
< |
shouldThrow(); |
1000 |
< |
} catch (FJException success) { |
1001 |
< |
} |
988 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
989 |
> |
public void realCompute() { |
990 |
> |
FailingFibAction f = new FailingFibAction(8); |
991 |
> |
FibAction g = new FibAction(9); |
992 |
> |
FibAction h = new FibAction(7); |
993 |
> |
HashSet set = new HashSet(); |
994 |
> |
set.add(f); |
995 |
> |
set.add(g); |
996 |
> |
set.add(h); |
997 |
> |
try { |
998 |
> |
invokeAll(set); |
999 |
> |
shouldThrow(); |
1000 |
> |
} catch (FJException success) { |
1001 |
> |
checkCompletedAbnormally(f, success); |
1002 |
|
} |
1003 |
< |
}; |
1004 |
< |
mainPool.invoke(a); |
1003 |
> |
}}; |
1004 |
> |
testInvokeOnPool(mainPool(), a); |
1005 |
|
} |
1006 |
|
|
1007 |
|
/** |
1009 |
|
* and suppresses execution |
1010 |
|
*/ |
1011 |
|
public void testTryUnfork() { |
1012 |
< |
RecursiveAction a = new RecursiveAction() { |
1013 |
< |
public void compute() { |
1014 |
< |
FibAction g = new FibAction(9); |
1015 |
< |
g.fork(); |
1016 |
< |
FibAction f = new FibAction(8); |
1017 |
< |
f.fork(); |
1018 |
< |
threadAssertTrue(f.tryUnfork()); |
1019 |
< |
helpQuiesce(); |
1020 |
< |
threadAssertFalse(f.isDone()); |
1021 |
< |
threadAssertTrue(g.isDone()); |
1022 |
< |
} |
1023 |
< |
}; |
867 |
< |
singletonPool.invoke(a); |
1012 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1013 |
> |
public void realCompute() { |
1014 |
> |
FibAction g = new FibAction(9); |
1015 |
> |
assertSame(g, g.fork()); |
1016 |
> |
FibAction f = new FibAction(8); |
1017 |
> |
assertSame(f, f.fork()); |
1018 |
> |
assertTrue(f.tryUnfork()); |
1019 |
> |
helpQuiesce(); |
1020 |
> |
checkNotDone(f); |
1021 |
> |
checkCompletedNormally(g); |
1022 |
> |
}}; |
1023 |
> |
testInvokeOnPool(singletonPool(), a); |
1024 |
|
} |
1025 |
|
|
1026 |
|
/** |
1028 |
|
* there are more tasks than threads |
1029 |
|
*/ |
1030 |
|
public void testGetSurplusQueuedTaskCount() { |
1031 |
< |
RecursiveAction a = new RecursiveAction() { |
1032 |
< |
public void compute() { |
1033 |
< |
FibAction h = new FibAction(7); |
1034 |
< |
h.fork(); |
1035 |
< |
FibAction g = new FibAction(9); |
1036 |
< |
g.fork(); |
1037 |
< |
FibAction f = new FibAction(8); |
1038 |
< |
f.fork(); |
1039 |
< |
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
1040 |
< |
helpQuiesce(); |
1041 |
< |
} |
1042 |
< |
}; |
1043 |
< |
singletonPool.invoke(a); |
1031 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1032 |
> |
public void realCompute() { |
1033 |
> |
FibAction h = new FibAction(7); |
1034 |
> |
assertSame(h, h.fork()); |
1035 |
> |
FibAction g = new FibAction(9); |
1036 |
> |
assertSame(g, g.fork()); |
1037 |
> |
FibAction f = new FibAction(8); |
1038 |
> |
assertSame(f, f.fork()); |
1039 |
> |
assertTrue(getSurplusQueuedTaskCount() > 0); |
1040 |
> |
helpQuiesce(); |
1041 |
> |
assertEquals(0, getSurplusQueuedTaskCount()); |
1042 |
> |
checkCompletedNormally(f); |
1043 |
> |
checkCompletedNormally(g); |
1044 |
> |
checkCompletedNormally(h); |
1045 |
> |
}}; |
1046 |
> |
testInvokeOnPool(singletonPool(), a); |
1047 |
|
} |
1048 |
|
|
1049 |
|
/** |
1050 |
|
* peekNextLocalTask returns most recent unexecuted task. |
1051 |
|
*/ |
1052 |
|
public void testPeekNextLocalTask() { |
1053 |
< |
RecursiveAction a = new RecursiveAction() { |
1054 |
< |
public void compute() { |
1055 |
< |
FibAction g = new FibAction(9); |
1056 |
< |
g.fork(); |
1057 |
< |
FibAction f = new FibAction(8); |
1058 |
< |
f.fork(); |
1059 |
< |
threadAssertTrue(peekNextLocalTask() == f); |
1060 |
< |
f.join(); |
1061 |
< |
threadAssertTrue(f.isDone()); |
1062 |
< |
helpQuiesce(); |
1063 |
< |
} |
1064 |
< |
}; |
1065 |
< |
singletonPool.invoke(a); |
1053 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1054 |
> |
public void realCompute() { |
1055 |
> |
FibAction g = new FibAction(9); |
1056 |
> |
assertSame(g, g.fork()); |
1057 |
> |
FibAction f = new FibAction(8); |
1058 |
> |
assertSame(f, f.fork()); |
1059 |
> |
assertSame(f, peekNextLocalTask()); |
1060 |
> |
assertNull(f.join()); |
1061 |
> |
checkCompletedNormally(f); |
1062 |
> |
helpQuiesce(); |
1063 |
> |
checkCompletedNormally(f); |
1064 |
> |
checkCompletedNormally(g); |
1065 |
> |
}}; |
1066 |
> |
testInvokeOnPool(singletonPool(), a); |
1067 |
|
} |
1068 |
|
|
1069 |
|
/** |
1071 |
|
* without executing it |
1072 |
|
*/ |
1073 |
|
public void testPollNextLocalTask() { |
1074 |
< |
RecursiveAction a = new RecursiveAction() { |
1075 |
< |
public void compute() { |
1076 |
< |
FibAction g = new FibAction(9); |
1077 |
< |
g.fork(); |
1078 |
< |
FibAction f = new FibAction(8); |
1079 |
< |
f.fork(); |
1080 |
< |
threadAssertTrue(pollNextLocalTask() == f); |
1081 |
< |
helpQuiesce(); |
1082 |
< |
threadAssertFalse(f.isDone()); |
1083 |
< |
} |
1084 |
< |
}; |
1085 |
< |
singletonPool.invoke(a); |
1074 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1075 |
> |
public void realCompute() { |
1076 |
> |
FibAction g = new FibAction(9); |
1077 |
> |
assertSame(g, g.fork()); |
1078 |
> |
FibAction f = new FibAction(8); |
1079 |
> |
assertSame(f, f.fork()); |
1080 |
> |
assertSame(f, pollNextLocalTask()); |
1081 |
> |
helpQuiesce(); |
1082 |
> |
checkNotDone(f); |
1083 |
> |
checkCompletedNormally(g); |
1084 |
> |
}}; |
1085 |
> |
testInvokeOnPool(singletonPool(), a); |
1086 |
|
} |
1087 |
|
|
1088 |
|
/** |
1089 |
< |
* pollTask returns an unexecuted task |
930 |
< |
* without executing it |
1089 |
> |
* pollTask returns an unexecuted task without executing it |
1090 |
|
*/ |
1091 |
|
public void testPollTask() { |
1092 |
< |
RecursiveAction a = new RecursiveAction() { |
1093 |
< |
public void compute() { |
1094 |
< |
FibAction g = new FibAction(9); |
1095 |
< |
g.fork(); |
1096 |
< |
FibAction f = new FibAction(8); |
1097 |
< |
f.fork(); |
1098 |
< |
threadAssertTrue(pollTask() == f); |
1099 |
< |
helpQuiesce(); |
1100 |
< |
threadAssertFalse(f.isDone()); |
1101 |
< |
threadAssertTrue(g.isDone()); |
1102 |
< |
} |
1103 |
< |
}; |
945 |
< |
singletonPool.invoke(a); |
1092 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1093 |
> |
public void realCompute() { |
1094 |
> |
FibAction g = new FibAction(9); |
1095 |
> |
assertSame(g, g.fork()); |
1096 |
> |
FibAction f = new FibAction(8); |
1097 |
> |
assertSame(f, f.fork()); |
1098 |
> |
assertSame(f, pollTask()); |
1099 |
> |
helpQuiesce(); |
1100 |
> |
checkNotDone(f); |
1101 |
> |
checkCompletedNormally(g); |
1102 |
> |
}}; |
1103 |
> |
testInvokeOnPool(singletonPool(), a); |
1104 |
|
} |
1105 |
|
|
1106 |
|
/** |
1107 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
1108 |
|
*/ |
1109 |
|
public void testPeekNextLocalTaskAsync() { |
1110 |
< |
RecursiveAction a = new RecursiveAction() { |
1111 |
< |
public void compute() { |
1112 |
< |
FibAction g = new FibAction(9); |
1113 |
< |
g.fork(); |
1114 |
< |
FibAction f = new FibAction(8); |
1115 |
< |
f.fork(); |
1116 |
< |
threadAssertTrue(peekNextLocalTask() == g); |
1117 |
< |
f.join(); |
1118 |
< |
helpQuiesce(); |
1119 |
< |
threadAssertTrue(f.isDone()); |
1120 |
< |
} |
1121 |
< |
}; |
1122 |
< |
asyncSingletonPool.invoke(a); |
1110 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1111 |
> |
public void realCompute() { |
1112 |
> |
FibAction g = new FibAction(9); |
1113 |
> |
assertSame(g, g.fork()); |
1114 |
> |
FibAction f = new FibAction(8); |
1115 |
> |
assertSame(f, f.fork()); |
1116 |
> |
assertSame(g, peekNextLocalTask()); |
1117 |
> |
assertNull(f.join()); |
1118 |
> |
helpQuiesce(); |
1119 |
> |
checkCompletedNormally(f); |
1120 |
> |
checkCompletedNormally(g); |
1121 |
> |
}}; |
1122 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
1123 |
|
} |
1124 |
|
|
1125 |
|
/** |
1126 |
< |
* pollNextLocalTask returns least recent unexecuted task |
1127 |
< |
* without executing it, in async mode |
1126 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
1127 |
> |
* executing it, in async mode |
1128 |
|
*/ |
1129 |
|
public void testPollNextLocalTaskAsync() { |
1130 |
< |
RecursiveAction a = new RecursiveAction() { |
1131 |
< |
public void compute() { |
1132 |
< |
FibAction g = new FibAction(9); |
1133 |
< |
g.fork(); |
1134 |
< |
FibAction f = new FibAction(8); |
1135 |
< |
f.fork(); |
1136 |
< |
threadAssertTrue(pollNextLocalTask() == g); |
1137 |
< |
helpQuiesce(); |
1138 |
< |
threadAssertTrue(f.isDone()); |
1139 |
< |
threadAssertFalse(g.isDone()); |
1140 |
< |
} |
1141 |
< |
}; |
984 |
< |
asyncSingletonPool.invoke(a); |
1130 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1131 |
> |
public void realCompute() { |
1132 |
> |
FibAction g = new FibAction(9); |
1133 |
> |
assertSame(g, g.fork()); |
1134 |
> |
FibAction f = new FibAction(8); |
1135 |
> |
assertSame(f, f.fork()); |
1136 |
> |
assertSame(g, pollNextLocalTask()); |
1137 |
> |
helpQuiesce(); |
1138 |
> |
checkCompletedNormally(f); |
1139 |
> |
checkNotDone(g); |
1140 |
> |
}}; |
1141 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
1142 |
|
} |
1143 |
|
|
1144 |
|
/** |
1145 |
< |
* pollTask returns an unexecuted task |
1146 |
< |
* without executing it, in async mode |
1145 |
> |
* pollTask returns an unexecuted task without executing it, in |
1146 |
> |
* async mode |
1147 |
|
*/ |
1148 |
|
public void testPollTaskAsync() { |
1149 |
< |
RecursiveAction a = new RecursiveAction() { |
1150 |
< |
public void compute() { |
1151 |
< |
FibAction g = new FibAction(9); |
1152 |
< |
g.fork(); |
1153 |
< |
FibAction f = new FibAction(8); |
1154 |
< |
f.fork(); |
1155 |
< |
threadAssertTrue(pollTask() == g); |
1156 |
< |
helpQuiesce(); |
1157 |
< |
threadAssertTrue(f.isDone()); |
1158 |
< |
threadAssertFalse(g.isDone()); |
1159 |
< |
} |
1160 |
< |
}; |
1004 |
< |
asyncSingletonPool.invoke(a); |
1149 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1150 |
> |
public void realCompute() { |
1151 |
> |
FibAction g = new FibAction(9); |
1152 |
> |
assertSame(g, g.fork()); |
1153 |
> |
FibAction f = new FibAction(8); |
1154 |
> |
assertSame(f, f.fork()); |
1155 |
> |
assertSame(g, pollTask()); |
1156 |
> |
helpQuiesce(); |
1157 |
> |
checkCompletedNormally(f); |
1158 |
> |
checkNotDone(g); |
1159 |
> |
}}; |
1160 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
1161 |
|
} |
1162 |
|
|
1163 |
|
} |