简体   繁体   English

与叉加入有关:join()vs get()vs invoke()

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

Is it necessary that I use join() with fork() or I may use also either of join() , get() , invoke() . 我是否需要将join()fork()使用,或者也可以使用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. 我检查了API ,除了get()抛出InterruptedExceptionExecutionException我看不出任何区别……而invoke()似乎完全一样。

However I have always seen related fork() with join() rather than the other two methods... don't they provide parallelism? 但是我一直看到与join()而不是其他两种方法相关的fork() ……它们不提供并行性吗? What's the purpose of having invoke() and join() totally the same? 具有完全相同的invoke()join()的目的是什么? I can understand get() got by implementing future, however what about invoke() and join(). 我可以理解实现将来会得到的get(),但是invoke()和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. 编辑 :我引用的API不好,因为已经收到的答案指出它对它说了些什么。 However what do they mean with: 但是,它们的含义是:

Method invoke() is semantically equivalent to fork(); 方法invoke()在语义上等效于fork(); join() but always attempts to begin execution in the current thread join()但总是尝试在当前线程中开始执行

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. 开始执行此任务,在必要时等待其完成,然后返回其结果,或者如果基础计算执行了此操作,则抛出(未经检查的)RuntimeException或Error。

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 . 此方法是从Future继承的,此方法类似于join From the javadoc for join : 从javadoc 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. 此方法与get()的不同之处在于,异常完成会导致RuntimeException或Error而不是ExecutionException,并且调用线程的中断不会导致该方法通过抛出InterruptedException突然返回。

So, to use the Fork/Join framework you need to call fork which is asynchronous. 因此,要使用Fork / Join框架,您需要调用异步的fork Then do the other part of the task locally. 然后在本地执行任务的另一部分。 Then call join . 然后调用join

The basic premise of the fork join framework is that it is used in divide and conquer algorithms that can be multi threaded. fork联接框架的基本前提是,它可以在多线程的分治算法中使用。

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 . 想法是将您的任务分成两个离散的单元,然后通过fork将一个任务传递给另一个ForkJoinTask这与当前Thread同时运行。 You then process the other unit in the current Thread . 然后,您可以处理当前Thread中的另一个单元。 When you are done you call join on the first task to ensure that you get the result from it. 完成后,您可以在第一个任务上调用join ,以确保从中获得结果。

Calling invoke waits for the invoked task to complete . 调用invoke 等待调用的任务完成 So your method in now not asynchronous. 因此,您的方法现在不是异步的。 You just run all of your parts sequentially somewhat defeating the point of fork/join. 您只需按顺序运行所有零件,这在一定程度上击败了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 . 因此,如果要调用x.fork().join() ,它将与x.invoke()相同,但要点是,您确实调用forkjoin 之间的当前Thread上进行工作。

It is written in the API doc you cited: 它是用您引用的API文档编写的:

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. 等待完成和提取任务结果的主要方法是join(),但有几种变体:Future.get()方法支持可中断和/或定时等待完成,并使用Future约定报告结果。 Method invoke() is semantically equivalent to fork(); 方法invoke()在语义上等效于fork(); join() but always attempts to begin execution in the current thread. join(),但始终尝试在当前线程中开始执行。 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. 方法invokeAll(有多个版本可用)执行并行调用的最常见形式:分派一组任务并将它们全部加入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM