ExecutorService.submit(Task)与CompletableFuture.supplyAsync(Task,Executor) [英] ExecutorService.submit(Task) vs CompletableFuture.supplyAsync(Task, Executor)

查看:259
本文介绍了ExecutorService.submit(Task)与CompletableFuture.supplyAsync(Task,Executor)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要并行或异步运行某些内容,可以使用ExecutorService:< T>未来< T>提交(可运行任务,T结果); 或CompletableFuture Api: static< U> CompletableFuture< U> supplyAsync(Supplier U供应商,执行人执行者);
(假设我在两种情况下都使用相同的执行人)

To run some stuff in parallel or asynchronously I can use either an ExecutorService: <T> Future<T> submit(Runnable task, T result); or the CompletableFuture Api:static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor); (Lets assume I use in both cases the same Executor)

除了返回类型 Future CompletableFuture 之外,还存在其他显着差异。还是什么时候使用什么?

Besides the return type Future vs. CompletableFuture are there any remarkable differences. Or When to use what?

如果我使用默认值为<$ c的 CompletableFuture API有什么区别? $ c>执行器(没有执行器的方法)?

And what are the differences if I use the CompletableFuture API with default Executor (the method without executor)?

推荐答案


除了返回类型Future与CompletableFuture之外,还存在其他显着差异。或者什么时候使用什么?

Besides the return type Future vs. CompletableFuture are there any remarkable differences. Or When to use what?

这真的很简单。当您希望执行线程等待异步计算响应时,可以使用 Future 。并行合并/排序就是一个例子。异步左排序,同步右排序,等待左完成( future.get()),合并结果。

It's rather simple really. You use the Future when you want the executing thread to wait for async computation response. An example of this is with a parallel merge/sort. Sort left asynchronously, sort right synchronously, wait on left to complete (future.get()), merge results.

当您要执行某些操作时,请使用 CompleteableFuture ,并在执行后的结果与执行的线程异步地执行。例如:我想异步进行一些计算,然后在计算时将结果写入某个系统。

You use a CompleteableFuture when you want some action executed, with the result after completion, asynchronously from the executed thread. For instance: I want to do some computation asynchronously and when I compute, write the results to some system. The requesting thread may not need to wait on a result then.

您可以在单个Future可执行文件中模仿以上示例,但 CompletableFuture 提供了更流畅的界面以及更好的错误处理。

You can mimic the above example in a single Future executable, but the CompletableFuture offers a more fluent interface with better error handling.

这实际上取决于您要执行的操作。

It really depends on what you want to do.


如果我将CompletableFutureApi与默认执行器(没有执行器的方法)一起使用,会有什么区别?

And what are the differences if i use the CompletableFutureApi with default Executor (the method without executor)?

它将委托给 ForkJoin.commonPool(),这是系统上CPU数量的默认大小。如果您正在执行IO密集型操作(读取和写入文件系统),则应以不同的方式定义线程池。

It will delegate to ForkJoin.commonPool() which is a default size to the number of CPUs on your system. If you are doing something IO intensive (reading and writing to the file system) you should define the thread pool differently.

如果CPU密集型,则使用commonPool最有意义

If it's CPU intensive, using the commonPool makes most sense.

这篇关于ExecutorService.submit(Task)与CompletableFuture.supplyAsync(Task,Executor)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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