[英]ForkJoinTask vs CompletableFuture
In Java 8 there are two ways of starting asynchronous computations - CompletableFuture
and ForkJoinTask
. 在Java 8中,有两种启动异步计算的方法 -
CompletableFuture
和ForkJoinTask
。 They both seem fairly similar - the inner classes of CompletableFuture
even extend ForkJoinTask
. 它们看起来非常相似 -
CompletableFuture
的内部类甚至扩展了ForkJoinTask
。
Is there a reason to use one over the other? 是否有理由使用其中一个?
One key difference that I can see is that the CompletableFuture.join
method simply blocks until the future is complete ( waitingGet
just spins using a ManagedBlocker
), whereas a ForkJoinTask.join
can steal work off the queue to help the task you're joining on to complete. 我可以看到的一个关键区别是
CompletableFuture.join
方法只是阻塞,直到将来完成( waitingGet
只是使用ManagedBlocker
旋转),而ForkJoinTask.join
可以从队列中窃取工作来帮助您加入的任务去完成。
Is there a benefit over one or the other? 有没有超过一个或另一个的好处?
They are two different things, ForkJoinTask
is a task that can be submitted to a ForkJoinPool
, CompletableFuture
is a promise that can work with any Executor
and the executor doesn't need to be the ForkJoinPool
, 这是两个不同的东西,
ForkJoinTask
是可以被提交给一个任务ForkJoinPool
, CompletableFuture
是可以与任何工作承诺Executor
与执行并不需要是ForkJoinPool
,
It is true however that the common ForkJoinPool
is the default if you don't specify any, for ex: 但是,如果你没有指定任何常见的
ForkJoinPool
,那么它是默认的,例如:
CompletableFuture.supplyAsync(()-> supplier);
uses the ForkJoinPool
if you don't pass an Executor
. 如果你没有通过
Executor
则使用ForkJoinPool
。 There is another overload
that takes an Executor
. 还有另一个
overload
需要Executor
。
CompletableFuture.supplyAsync(()-> supplier,executor);
Async
,which is a static
class in CompletableFuture
extends ForkJoinTask<Void>
, but it doesn't need to be a ForkJoinTask
, from the docs of Async
Async
,它是CompletableFuture
的static
类,扩展了ForkJoinTask<Void>
,但它不需要是来自Async
文档的ForkJoinTask
/** Base class can act as either FJ or plain Runnable */
/ **基类可以充当FJ或普通Runnable * /
abstract static class Async extends ForkJoinTask<Void>
implements Runnable, AsynchronousCompletionTask
It can also a Runnable
and a AsynchronousCompletionTask
它也可以是
Runnable
和AsynchronousCompletionTask
Just on side note: ForkJoinTask
, ForkJoinPool
, ForkJoin...
classes were added in 1.7 and not 1.8 旁注:
ForkJoinTask
, ForkJoinPool
, ForkJoin...
类在1.7而不是1.8中添加
I'd say that the ForkJoinTask
is more recommended when you have a big task and want to split it to run in parallel in several sub tasks. 我会说当你有一个大任务并希望将它拆分为在几个子任务中并行运行时,更推荐使用
ForkJoinTask
。 The ForkJoin
framework uses the work stealing algorithm which will make an efficient use of the threads. ForkJoin
框架使用工作窃取算法,可以有效地使用线程。 On the other hand, CompletableFutures
are more appropriate for a Reactive programming model, where you can create pipelines of execution in a sync or async fashion, and have a better control of the threads by using the thread pools of the Executor Service. 另一方面,
CompletableFutures
更适合于Reactive编程模型,您可以在其中以同步或异步方式创建执行流水线,并通过使用Executor Service的线程池更好地控制线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.