While I was reading the javadoc about ForkJoinTask I came across the following statement:
This method may be invoked only from within
ForkJoinPoolcomputations (as may be determined using methodinForkJoinPool()). Attempts to invoke in other contexts result in exceptions or errors, possibly includingClassCastException.
What's not clear to me is what ForkJoinContext means here. I know what static context means, for example. It clearly defines in the JLS 8.1.3 as follows (Emphasize's mine):
A statement or expression occurs in a static context if and only if the innermost method, constructor, instance initializer, static initializer, field initializer, or explicit constructor invocation statement enclosing the statement or expression is a static method, a static initializer, the variable initializer of a static variable, or an explicit constructor invocation statement (§8.8.7).
The context being discussed here is not static/non-static context.
It means that the method must be called from a thread which belongs to the ForkJoinPool.
So it must be called from a recursive task. Calling from anywhere else might cause exceptions/errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With