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.
The Fork/Join framework in Java 7 is an implementation of the Divide and Conquer algorithm, in which a central ForkJoinPool executes branching ForkJoinTasks. ExecutorService is an Executor that provides methods to manage the progress-tracking and termination of asynchronous tasks.
The fork is responsible for splitting the task, and join is responsible for merging the results of the task to generate the final result. It is worth noting here that various threads that are responsible for the completion of the sub-tasks never sit idle.
The fork/join framework was designed to speed up the execution of tasks that can be divided into other smaller subtasks, executing them in parallel and then combining their results to get a single one.
commonPool. public static ForkJoinPool commonPool() Returns the common pool instance. This pool is statically constructed; its run state is unaffected by attempts to shutdown() or shutdownNow() . However this pool and any ongoing processing are automatically terminated upon program System.
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
.
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