简体   繁体   English

ForkJoinTask - join() 与 invoke()

[英]ForkJoinTask - join() vs invoke()

Java docs says next for join() : Java 文档说接下来是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 突然返回。

And for invoke() :对于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 或错误。

But even after reading docs I can't understand difference between them.但即使阅读了文档,我也无法理解它们之间的区别。 Both of them waits for future to return result, as I understand they handles exceptions in different ways?他们都在等待未来返回结果,据我了解他们以不同的方式处理异常?

In the ForkJoinTask API docs,在 ForkJoinTask 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.这些方法的“安静”forms 不提取结果或报告异常。 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(在多个版本中可用)执行最常见的并行调用形式:分叉一组任务并将它们全部加入。

In simple words, invoke() == fork();join();简单来说,invoke() == fork();join();

fork() itself is asynchronous where it split the tasks two work units and passes the one unit to another ForkJoinTask, and the number of ForkJoinTask available is defined by your ForkJoinPool. fork() 本身是异步的,它将任务分成两个工作单元并将一个单元传递给另一个 ForkJoinTask,可用的 ForkJoinTask 数量由您的 ForkJoinPool 定义。 And, you will call join()/get() to wait for it to complete.并且,您将调用 join()/get() 以等待它完成。

Meanwhile, for invoke(), it is synchronous and the ForkJoinTask is invoke at the main thread, blocking the main thread immediately.同时,对于invoke(),它是同步的,在主线程调用ForkJoinTask,立即阻塞主线程。 If you need to execute something in between fork() and join() you couldn't.如果您需要在 fork() 和 join() 之间执行某些操作,则您不能。

So my advise is use invoke() when you have nothing to do in between fork() & join() and you just need pure multi-threading capabilities.所以我的建议是当您在 fork() 和 join() 之间无事可做并且您只需要纯多线程功能时使用 invoke() 。 If you want advanced joining between ForkJoinTasks or execute something in between, then use fork();join();如果您想在 ForkJoinTasks 之间进行高级连接或在两者之间执行某些操作,请使用 fork();join();

But keep in mind, both methods does not means that your tasks get to split into another thread.但请记住,这两种方法并不意味着您的任务会拆分到另一个线程中。 It depends on the number of physical thread in your machine and the number of pools initialized for your ForkJoinPool.这取决于您机器中的物理线程数和为您的 ForkJoinPool 初始化的池数。

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

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