Fork-Join related: join() vs get() vs invoke()
Asked Answered
A

2

10

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.

Araroba answered 26/7, 2013 at 8:6 Comment(0)
O
11

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.

Octavla answered 26/7, 2013 at 8:10 Comment(13)
Invoke() it's not a void methodAraroba
@Rollerball, sorry; was reading wrong method description. The rest is correct.Octavla
Still don't get it man.. join() too waits for the task to complete.. fork() just create a new process and push the task within itself got called to its worker queue.Araroba
But you call fork then you do something then call join. So you do two things at once. If you call invoke you can only ever do one thing at once.Octavla
and what if I use fork-invoke instead of fork-join? Should it be the same?Araroba
They are different. fork starts a process asynchronously, invoke starts a process synchronously. join/get wait for a process to finish. So fork-invoke would start a process twice, once in the background and once in the foreground.Octavla
So fork() just push the calling task to a different worker queue and join just wait for it to be performed. It it's like that i have understood everything I have been trying to understand.Araroba
You are correct. fork puts the task onto another Thread in the ForkJoinPool and join waits for it to finish.Octavla
And the last thing.. how does the system understand when to use another processor (which is the point of using fork-join)? If all it does is putting the task onto another Thread.. when does the process thing actually take place?Araroba
The ForkJoinPool is a thread pool, by default with the same number of threads as you have CPUs. This works as a Producer/Consumer pattern with you producing tasks by calling fork and the threads running the tasks.Octavla
Ok thanks for your time.. However Also a FixedThreadPool can use the same amount of thread but it does not guarantee that it will be multiprocessor.. which is in the ForkJoinPool. I wonder how can it be guarantee in the ForkJoinPool, the use of more CPUsAraroba
A ForkJoinPool tries to ensure that all Threads are busy. It is essentially an extended ExecutorService. Which thread runs on which CPU is down the OS' task scheduler.Octavla
So it's not true that guarantees any multi-processing work any more than a normal FixedThreadPool(4) for example..Araroba
S
0

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.

Shikoku answered 26/7, 2013 at 8:9 Comment(1)
What do they actually mean with "join() but always attempts to begin execution in the current thread."Araroba

© 2022 - 2024 — McMap. All rights reserved.