What is the difference between get() and invoke() in Java 7's ForkJoinTask?

Go To StackoverFlow.com

3

Here is the javadoc for both:

  1. get(): Waits if necessary for the computation to complete, and then retrieves its result.
  2. invoke(): Commences performing this task, awaits its completion if necessary, and returns its result, or throws an (unchecked) RuntimeException or Error if the underlying computation did so.
2012-04-05 02:52
by pathikrit


3

get() supports interruptible and/or timed waits for completion and report results using Future conventions. Method invoke() is semantically equivalent to fork(); join() but always attempts to begin execution in the current thread.

2012-04-05 03:26
by NiranjanBhat
If I don't care about timing/interruptions and I know for sure my get() call won't thrown an ExecutionException should I just use invoke() then or does invoke() have some other downsides? Which one is better performance wise - my use case involves submitting thousands of I/O bound ForkJoinTasks to a static ForkJoinPool and I have to decide whether I should call get() or invoke() on the ForkJoinTask returned by the submit() method of the ForkJoinPool - pathikrit 2012-04-05 04:18
Wait a minute! ForkJoinTask has this restriction "Tasks should also not perform blocking IO". If you know about this restriction and still want to go ahead, considering performance as your main criteria, I would suggest to use get() as this will be executed directly by the pool, unlike invoke() where it would first attempt to invoke execution on current thread - NiranjanBhat 2012-04-05 04:46
Ads