Fork-Join related: join() vs get() vs invoke()

2019-02-23 08:05发布

问题:

Is it necessary that I use join() with fork() or I may use also either of join(), get(), invoke(). I checked the API and besides that get() throws InterruptedException and ExecutionException I don't see differences... And invoke() seems totally the same.

However I have always seen related fork() with join() rather than the other two methods... don't they provide parallelism? What's the purpose of having invoke() and join() totally the same? I can understand get() got by implementing future, however what about invoke() and join(). Thanks in advance.

EDIT: My bad in the API i quoted actually it says something about it as the already received answers pointed out. However what do they mean with:

Method invoke() is semantically equivalent to fork(); join() but always attempts to begin execution in the current thread

Thanks in advance.

回答1:

Why don't you read the documentation that you linked to?

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.

Seems pretty clear to me, awaits its completion if necessary is rather unambiguously saying that this method is not asynchronous.

get

Waits if necessary for the computation to complete, and then retrieves its result.

This method is inherited from Future, this method is analogous to join. From the javadoc for join:

Returns the result of the computation when it is done. This method differs from get() in that abnormal completion results in RuntimeException or Error, not ExecutionException, and that interrupts of the calling thread do not cause the method to abruptly return by throwing InterruptedException.

So, to use the Fork/Join framework you need to call fork which is asynchronous. Then do the other part of the task locally. Then call join.

The basic premise of the fork join framework is that it is used in divide and conquer algorithms that can be multi threaded.

The idea is that you split you task into two discrete units and then pass one off to another ForkJoinTask via fork - this runs concurrently to the current Thread. You then process the other unit in the current Thread. When you are done you call join on the first task to ensure that you get the result from it.

Calling invoke waits for the invoked task to complete. So your method in now not asynchronous. You just run all of your parts sequentially somewhat defeating the point of fork/join.

So if you were to call x.fork().join() it would be the same as x.invoke() but the whole point is that you do work on the current Thread between invoking fork and join.



回答2:

It is written in the API doc you cited:

The primary method for awaiting completion and extracting results of a task is join(), but there are several variants: The Future.get() methods support 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. The "quiet" forms of these methods do not extract results or report exceptions. These may be useful when a set of tasks are being executed, and you need to delay processing of results or exceptions until all complete. Method invokeAll (available in multiple versions) performs the most common form of parallel invocation: forking a set of tasks and joining them all.