简体   繁体   English

Java CompletionStage/CompletableFuture 苦苦挣扎

[英]Struggling with Java CompletionStage/CompletableFuture

Looking at a the docs and a couple of examples, im still a little confused.看着文档和几个例子,我还是有点困惑。 I think I get the bare basics of it, but Im confused on when one would use them.我想我了解了它的基本知识,但我对何时使用它们感到困惑。

I guess my main questions..我想我的主要问题..

  1. Are CompletionStage/CompletableFuture both for async code? CompletionStage/CompletableFuture 都适用于异步代码吗? If so, why would you use one over the other?如果是这样,你为什么要使用一个而不是另一个?
  2. If CompletionStage can be used for non-async code, why even use it at all?如果 CompletionStage 可以用于非异步代码,为什么还要使用它呢? How would it be any different than just standard sequential code?它与标准顺序代码有何不同?
  3. How are they even different?他们甚至有什么不同? I see there are asynchronous methods you can call for both CompletionStage and CompletableFuture.我看到您可以为 CompletionStage 和 CompletableFuture 调用异步方法。
  4. What scenario would you use one over the other?您会在哪种情况下使用其中一种?

CompletionStage is an interface that (1) can be implemented by any number of classes and (2) does not specify detailed policies such as the thread allocation of asynchronous operations. CompletionStage是一个接口,(1) 可以由任意数量的类实现,(2) 不指定异步操作的线程分配等详细策略。

CompletableFuture is a class that implements CompletionStage (and Future ) and does provide more detail about those policies. CompletableFuture是一个class ,它实现了CompletionStage (和Future )并且确实提供了有关这些策略的更多详细信息。

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

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