The API says:
“Method invokeAll
(available in multiple versions) performs the most common form of parallel invocation: forking a set of tasks and joining them all.”
So my assumption is if I use invokeAll()
, I no longer need to use fork()
or join()
methods. I also know that in general, join()
is called only if the task returns a value: a subclass of: RecursiveTask
and not used if the task not returning a value: subclass of: RecursiveAction
.
I came across this tutorial which has called ForkJoinTask.join()
after its called: invokeAll()
:
https://www.baeldung.com/java-fork-join
@Override
protected Integer compute() {
if (arr.length > THRESHOLD) {
return ForkJoinTask.invokeAll(createSubtasks())
.stream()
.mapToInt(ForkJoinTask::join) // join() is called
.sum();
} else {
return processing(arr);
}
}
Also this post, which has used join()
after calling invokeAll()
:
Why should we call join after invokeAll method?
invokeAll(subTask1, subTask2);
return subTask1.join() + subTask2.join();
At the same time, I looked at many other examples for invokeAll()
and there is NO join()
after that call.
1) Is there any rule that when we should or should not use join()
after invokeAll()
?
2) invokeAll()
comes in three different signatures, is this depending on the signature of the method to decide whether to use join()
or not?
3) On the same note, I also read this in the API:
"Method invoke() is semantically equivalent to fork(); join() but always attempts to begin execution in the current thread."
Is it saying that invoke()
is equal to fork()
plus join()
?