33 |
|
Math.max(2, Runtime.getRuntime().availableProcessors()); |
34 |
|
|
35 |
|
/** |
36 |
< |
* Analog of CheckedRunnable for CountedCompleter |
36 |
> |
* Analog of CheckedRunnable for ForkJoinTasks |
37 |
|
*/ |
38 |
|
public abstract class CheckedFJTask extends RecursiveAction { |
39 |
|
protected abstract void realCompute() throws Throwable; |
217 |
|
FJException() { super(); } |
218 |
|
} |
219 |
|
|
220 |
– |
static abstract class FailingCCF extends CountedCompleter { |
221 |
– |
int number; |
222 |
– |
int rnumber; |
223 |
– |
|
224 |
– |
public FailingCCF(CountedCompleter parent, int n) { |
225 |
– |
super(parent, 1); |
226 |
– |
this.number = n; |
227 |
– |
} |
228 |
– |
|
229 |
– |
public final void compute() { |
230 |
– |
CountedCompleter p; |
231 |
– |
FailingCCF f = this; |
232 |
– |
int n = number; |
233 |
– |
while (n >= 2) { |
234 |
– |
new RFCCF(f, n - 2).fork(); |
235 |
– |
f = new LFCCF(f, --n); |
236 |
– |
} |
237 |
– |
f.number = n; |
238 |
– |
f.onCompletion(f); |
239 |
– |
if ((p = f.getCompleter()) != null) |
240 |
– |
p.tryComplete(); |
241 |
– |
else |
242 |
– |
f.quietlyComplete(); |
243 |
– |
} |
244 |
– |
} |
245 |
– |
|
246 |
– |
static final class LFCCF extends FailingCCF { |
247 |
– |
public LFCCF(CountedCompleter parent, int n) { |
248 |
– |
super(parent, n); |
249 |
– |
} |
250 |
– |
public final void onCompletion(CountedCompleter caller) { |
251 |
– |
FailingCCF p = (FailingCCF)getCompleter(); |
252 |
– |
int n = number + rnumber; |
253 |
– |
if (p != null) |
254 |
– |
p.number = n; |
255 |
– |
else |
256 |
– |
number = n; |
257 |
– |
} |
258 |
– |
} |
259 |
– |
static final class RFCCF extends FailingCCF { |
260 |
– |
public RFCCF(CountedCompleter parent, int n) { |
261 |
– |
super(parent, n); |
262 |
– |
} |
263 |
– |
public final void onCompletion(CountedCompleter caller) { |
264 |
– |
completeExceptionally(new FJException()); |
265 |
– |
} |
266 |
– |
} |
267 |
– |
|
268 |
– |
static abstract class CCF extends CountedCompleter { |
269 |
– |
int number; |
270 |
– |
int rnumber; |
271 |
– |
|
272 |
– |
public CCF(CountedCompleter parent, int n) { |
273 |
– |
super(parent, 1); |
274 |
– |
this.number = n; |
275 |
– |
} |
276 |
– |
|
277 |
– |
public final void compute() { |
278 |
– |
CountedCompleter p; |
279 |
– |
CCF f = this; |
280 |
– |
int n = number; |
281 |
– |
while (n >= 2) { |
282 |
– |
new RCCF(f, n - 2).fork(); |
283 |
– |
f = new LCCF(f, --n); |
284 |
– |
} |
285 |
– |
f.number = n; |
286 |
– |
f.onCompletion(f); |
287 |
– |
if ((p = f.getCompleter()) != null) |
288 |
– |
p.tryComplete(); |
289 |
– |
else |
290 |
– |
f.quietlyComplete(); |
291 |
– |
} |
292 |
– |
} |
293 |
– |
|
294 |
– |
static final class LCCF extends CCF { |
295 |
– |
public LCCF(CountedCompleter parent, int n) { |
296 |
– |
super(parent, n); |
297 |
– |
} |
298 |
– |
public final void onCompletion(CountedCompleter caller) { |
299 |
– |
CCF p = (CCF)getCompleter(); |
300 |
– |
int n = number + rnumber; |
301 |
– |
if (p != null) |
302 |
– |
p.number = n; |
303 |
– |
else |
304 |
– |
number = n; |
305 |
– |
} |
306 |
– |
} |
307 |
– |
static final class RCCF extends CCF { |
308 |
– |
public RCCF(CountedCompleter parent, int n) { |
309 |
– |
super(parent, n); |
310 |
– |
} |
311 |
– |
public final void onCompletion(CountedCompleter caller) { |
312 |
– |
CCF p = (CCF)getCompleter(); |
313 |
– |
int n = number + rnumber; |
314 |
– |
if (p != null) |
315 |
– |
p.rnumber = n; |
316 |
– |
else |
317 |
– |
number = n; |
318 |
– |
} |
319 |
– |
} |
320 |
– |
|
220 |
|
static final class NoopCountedCompleter extends CountedCompleter { |
221 |
|
boolean post; // set true if onCompletion called |
222 |
|
NoopCountedCompleter() { super(); } |
410 |
|
assertTrue(a.isDone()); |
411 |
|
assertFalse(b.isDone()); |
412 |
|
} |
413 |
+ |
|
414 |
+ |
// Invocation tests use some interdependent task classes |
415 |
+ |
// to better test propagation etc |
416 |
+ |
|
417 |
+ |
|
418 |
+ |
// Version of Fibonacci with different classes for left vs right forks |
419 |
+ |
static abstract class CCF extends CountedCompleter { |
420 |
+ |
int number; |
421 |
+ |
int rnumber; |
422 |
+ |
|
423 |
+ |
public CCF(CountedCompleter parent, int n) { |
424 |
+ |
super(parent, 1); |
425 |
+ |
this.number = n; |
426 |
+ |
} |
427 |
+ |
|
428 |
+ |
public final void compute() { |
429 |
+ |
CountedCompleter p; |
430 |
+ |
CCF f = this; |
431 |
+ |
int n = number; |
432 |
+ |
while (n >= 2) { |
433 |
+ |
new RCCF(f, n - 2).fork(); |
434 |
+ |
f = new LCCF(f, --n); |
435 |
+ |
} |
436 |
+ |
f.number = n; |
437 |
+ |
f.onCompletion(f); |
438 |
+ |
if ((p = f.getCompleter()) != null) |
439 |
+ |
p.tryComplete(); |
440 |
+ |
else |
441 |
+ |
f.quietlyComplete(); |
442 |
+ |
} |
443 |
+ |
} |
444 |
+ |
|
445 |
+ |
static final class LCCF extends CCF { |
446 |
+ |
public LCCF(CountedCompleter parent, int n) { |
447 |
+ |
super(parent, n); |
448 |
+ |
} |
449 |
+ |
public final void onCompletion(CountedCompleter caller) { |
450 |
+ |
CCF p = (CCF)getCompleter(); |
451 |
+ |
int n = number + rnumber; |
452 |
+ |
if (p != null) |
453 |
+ |
p.number = n; |
454 |
+ |
else |
455 |
+ |
number = n; |
456 |
+ |
} |
457 |
+ |
} |
458 |
+ |
static final class RCCF extends CCF { |
459 |
+ |
public RCCF(CountedCompleter parent, int n) { |
460 |
+ |
super(parent, n); |
461 |
+ |
} |
462 |
+ |
public final void onCompletion(CountedCompleter caller) { |
463 |
+ |
CCF p = (CCF)getCompleter(); |
464 |
+ |
int n = number + rnumber; |
465 |
+ |
if (p != null) |
466 |
+ |
p.rnumber = n; |
467 |
+ |
else |
468 |
+ |
number = n; |
469 |
+ |
} |
470 |
+ |
} |
471 |
+ |
|
472 |
+ |
// Version of CCF with forced failure in left completions |
473 |
+ |
static abstract class FailingCCF extends CountedCompleter { |
474 |
+ |
int number; |
475 |
+ |
int rnumber; |
476 |
+ |
|
477 |
+ |
public FailingCCF(CountedCompleter parent, int n) { |
478 |
+ |
super(parent, 1); |
479 |
+ |
this.number = n; |
480 |
+ |
} |
481 |
+ |
|
482 |
+ |
public final void compute() { |
483 |
+ |
CountedCompleter p; |
484 |
+ |
FailingCCF f = this; |
485 |
+ |
int n = number; |
486 |
+ |
while (n >= 2) { |
487 |
+ |
new RFCCF(f, n - 2).fork(); |
488 |
+ |
f = new LFCCF(f, --n); |
489 |
+ |
} |
490 |
+ |
f.number = n; |
491 |
+ |
f.onCompletion(f); |
492 |
+ |
if ((p = f.getCompleter()) != null) |
493 |
+ |
p.tryComplete(); |
494 |
+ |
else |
495 |
+ |
f.quietlyComplete(); |
496 |
+ |
} |
497 |
+ |
} |
498 |
+ |
|
499 |
+ |
static final class LFCCF extends FailingCCF { |
500 |
+ |
public LFCCF(CountedCompleter parent, int n) { |
501 |
+ |
super(parent, n); |
502 |
+ |
} |
503 |
+ |
public final void onCompletion(CountedCompleter caller) { |
504 |
+ |
FailingCCF p = (FailingCCF)getCompleter(); |
505 |
+ |
int n = number + rnumber; |
506 |
+ |
if (p != null) |
507 |
+ |
p.number = n; |
508 |
+ |
else |
509 |
+ |
number = n; |
510 |
+ |
} |
511 |
+ |
} |
512 |
+ |
static final class RFCCF extends FailingCCF { |
513 |
+ |
public RFCCF(CountedCompleter parent, int n) { |
514 |
+ |
super(parent, n); |
515 |
+ |
} |
516 |
+ |
public final void onCompletion(CountedCompleter caller) { |
517 |
+ |
completeExceptionally(new FJException()); |
518 |
+ |
} |
519 |
+ |
} |
520 |
|
|
521 |
|
/** |
522 |
|
* invoke returns when task completes normally. |