Java ExecutorService.submit(Task) vs CompletableFuture.supplyAsync(Task, Executor)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39472061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:10:43  来源:igfitidea点击:

ExecutorService.submit(Task) vs CompletableFuture.supplyAsync(Task, Executor)

javaconcurrencyfuturecompletable-future

提问by dermoritz

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)

要并行或异步运行某些东西,我可以使用 ExecutorService:<T> Future<T> submit(Runnable task, T result);或 CompletableFuture Api:(static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor);假设我在两种情况下都使用相同的 Executor)

Besides the return type Futurevs. CompletableFutureare there any remarkable differences. Or When to use what?

除了返回类型Futurevs.CompletableFuture是否有任何显着差异。或者什么时候用什么?

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

如果我使用CompletableFuture默认的API Executor(没有执行程序的方法)有什么区别?

采纳答案by John Vint

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

除了返回类型 Future 和 CompletableFuture 之外,还有什么显着的不同。或者什么时候用什么?

It's rather simple really. You use the Futurewhen 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.

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

You use a CompleteableFuturewhen 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.

CompleteableFuture当您想要执行某些操作时使用 a ,并在完成后从执行的线程异步获得结果。例如:我想异步进行一些计算,当我计算时,将结果写入某个系统。请求线程可能不需要等待结果。

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

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

It really depends on what you want to do.

这真的取决于你想做什么。

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

如果我使用 CompletableFutureApi 和默认 Executor(没有执行器的方法)有什么区别?

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.

它会将ForkJoin.commonPool()默认大小委托给您系统上的 CPU 数量。如果你正在做一些 IO 密集型的事情(读取和写入文件系统),你应该以不同的方式定义线程池。

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

如果它是 CPU 密集型的,使用 commonPool 最有意义。

回答by Chinmay Phadke

CompletableFuture has rich features like chaining multiple futures, combining the futures, executing some action after future is executed (both synchronously as well as asynchronously), etc.

CompletableFuture 具有多种功能,例如链接多个期货、组合期货、执行期货后执行某些操作(同步和异步)等。

However, CompletableFuture is no different than Future in terms of performance. Even when combine multiple instances of CompletableFuture (using .thenCombine and .join in the end), none of them get executed unless we call .get method and during this time, the invoking thread is blocked. I feel in terms of performance, this is not better than Future.

但是,CompletableFuture 在性能方面与 Future 没有什么不同。即使组合多个 CompletableFuture 实例(最后使用 .thenCombine 和 .join),除非我们调用 .get 方法,否则它们都不会被执行,并且在此期间,调用线程被阻塞。我觉得在性能方面,这并不比 Future 好。

Please let me know if I am missing some aspect of performance here.

如果我在这里遗漏了某些方面的性能,请告诉我。

回答by dermoritz

This clarified for me the difference between future an completable future a bit more: Difference between Future and Promise

这对我来说澄清了未来与可完成未来之间的区别:未来和承诺之间的区别

CompletableFuture is more like a promise.

CompletableFuture 更像是一个承诺。