[concurrency-interest] Composing ForkJoin tasks

Doug Lea dl at cs.oswego.edu
Wed Jul 22 10:42:22 EDT 2009


Rich Hickey wrote:
> There are many instances in FJ where it states "This method may be
> invoked only from within ForkJoinTask computations." Is
> ForkJoinTask.getPool() the correct way to detect when one is within a
> ForkJoinTask computation?

Yes. The reason there is no other method is just that
no name for such a method seems to be better than writing
if (ForkJoinTask.getPool() == null)...

If you can think of one, feel free to suggest it.

> 
> It seems desirable if one is in a ForkJoin task and needs to execute
> others, it would be preferable to fork/invokeAll them rather than use
> a fresh ForkJoinPool.execute, or does it not matter much?

It is definitely faster, although ...

> 
> Obviously in a centrally-authored nested computation one knows what's
> going on, the situation I'm talking about is arbitrary nested
> composition of generic things like parallel map functions.

... in the case of a full nested parallel computation, it is
not as likely to matter as much.

> 
> Is ForkJoinTask.getPool() guaranteed to be safe and valid from any
> thread, and a sufficient indicator of running "within a ForkJoinTask
> computation"? If so, I think perhaps its doc string could better
> indicate that, something like:
> 
> "Returns the pool hosting the current task execution, or null if the
> current thread is executing outside of any ForkJoin computation."
> 

Done; thanks.

-Doug


More information about the Concurrency-interest mailing list