Fork-Join相关:join()vs get()vs invoke() [英] Fork-Join related: join() vs get() vs invoke()

查看:273
本文介绍了Fork-Join相关:join()vs get()vs invoke()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否需要使用 join() fork()或者我也可以使用 join() get() invoke()
我选中了 API 以及 get() throws InterruptedException ExecutionException 我看不到差异...和 invoke()看起来完全一样。



但我总是看到相关 fork() join(),而不是其他两个方法...他们不提供并行性吗?具有 invoke() join()的目的是什么?我可以理解get()通过实现未来,然而关于invoke()和join()。提前感谢。



编辑:我在API中的错误引用,实际上它说的是已经收到的答案。但是它们是什么意思:


方法invoke()在语义上等同于fork



预先感谢。

解决方案

为什么不阅读您链接到的文档?


调用



开始执行此任务,必要时等待其完成,
并返回其结果,或者抛出一个(未选中)RuntimeException或$ b


看起来很清楚,如有必要,等待完成 >




$ b

等待计算完成,然后检索
其结果。


此方法继承自 Future ,此方法类似于 join 。从的javadoc :


返回计算结果完成。此方法
与get()不同,因为异常完成导致
RuntimeException或Error,而不是ExecutionException,并且调用线程的中断
不会导致方法突然返回
throwing InterruptedException。


因此,要使用Fork / Join框架,你需要调用 code>是异步的。然后在本地执行任务的其他部分。然后调用 join



fork连接框架的基本前提是它用于分割和克服算法可以是多线程的。



这个想法是你将任务分成两个独立的单元,然后将一个关闭到另一个 ForkJoinTask 通过 fork - 这并发运行到当前线程。然后,在当前线程中处理另一个单元。完成后,在第一个任务上调用 join ,以确保从中获取结果。



调用 invoke 等待被调用的任务完成。所以你的方法现在异步。



所以如果你调用 x.fork()。 join()它将与 x.invoke()相同,但是整个问题是你在当前的 之间

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.

解决方案

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.

这篇关于Fork-Join相关:join()vs get()vs invoke()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆