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

Do I need to use join() with fork() , or can I also use either join() , get() , invoke() . I checked the API and besides the fact that get() throws InterruptedException and ExecutionException I see no differences ... And invoke() seems to be exactly the same.

However, I have always seen fork() associated with join() , and not with the other two methods ... do they provide parallelism? What is the goal of having invoke() and join() completely the same thing? I understand that get () got an implementation of the future, but what about invoke () and join (). Thanks in advance.

EDIT : my bad one in the API that I quoted, it actually says something about it, as the answers already received pointed out. However, what they mean:

The invoke () method is semantically equivalent to fork (); join (), but always trying to start execution on the current thread

Thanks in advance.

+4
source share
2 answers

Why don't you read the documentation you contacted?

call

Starts to perform this task, waits for its completion, if necessary, and returns its result, or throws a (uncontrolled) RuntimeException or Error if the underlying calculation did this.

It seems to me pretty understandable, waiting for it to complete, if necessary, pretty clearly says that this method is not asynchronous.

will arrive

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

This method inherits from Future , this method is similar to join . From javadoc to join :

Returns the result of the calculation when this is done. This method differs from get () in that abnormal termination results in a RuntimeException or Error rather than an ExecutionException, and that interrupting the calling thread does not cause the method to throw throwing InterruptedException suddenly.

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

The basic premise of the fork framework is that it is used in separation and subjugation algorithms, which can be multi-threaded.

The idea is that you split the task into two discrete units, and then transfer one of them to the other ForkJoinTask via fork - this is done simultaneously with the current Thread . Then you process another block in the current Thread . When you are done, you call join on the first task to make sure you get the result from it.

The invoke call is waiting for the completed task to complete . So your method is now not asynchronous. You simply run all of your parts sequentially, breaking the fork / join point somewhat.

So, if you were to call x.fork().join() , it would be the same as x.invoke() , but the thing is that you are working on the current Thread between calling fork and join .

+6
source

This is written in the API document you specified:

The main method of waiting for completion and retrieving the results of a task is join (), but there are several options: Future.get () methods support interruption and / or time-outs to complete and retrieve report results using Future conventions. The invoke () method is semantically equivalent to fork (); join (), but always tries to start execution on the current thread. The silent forms of these methods do not retrieve results or report exceptions. They can be useful when a set of tasks is performed, and you need to defer processing of results or exceptions until everything is complete. The invokeAll method (available in several versions) performs the most common form of parallel calling: expands a set of tasks and attaches them all.

0
source

Source: https://habr.com/ru/post/1493604/


All Articles