I have read that the following code:
invokeAll(a2, a1);
is equivalent to:
a2.compute();
a1.join();
in a RecursiveAction in a Fork-Join.
However, since RecursiveTask returns a value we may have something like:
Integer result1 = t2.compute();
Integer result2 = t1.join();
And then we should merge result1 and result2 to produce the final result.
Now, my doubts are:
Can invokedAll be used for RecursiveTasks?
If so, how can the result of invokeAll calls be merged to return the final result?
The invokeAll
method can be used for RecursiveTask
s. It takes two ForkJoinTask
s as parameters, and because RecursiveTask
extends ForkJoinTask
, a RecursiveTask
can be used as a ForkJoinTask
.
As for getting the results, invokeAll
doesn't return anything, so you're not going to get the information that way. What you can do is call the join
method on each of your RecursiveTask
s. Because you know that both tasks have completed when invokeAll
returns, the join
method will return very quickly.
invokeAll(t1, t2);
Integer result1 = t1.join();
Integer result2 = t2.join();
However, at this point, you may as well just do the thing you were doing originally, calling compute
and join
. Using invokeAll
is a bit clumsy when you only have two tasks and you need both of their results.