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