CompletableFuture如何先返回FALSE或等到全部完成后再返回TRUE [英] CompletableFuture how to return first FALSE or wait until all are completed to return TRUE
问题描述
我有些奇怪的情况,似乎不允许这种钉子钉入任何已广泛建立的CompletableFuture孔中.
I have a bit of an odd situation that doesn’t seem to allow this peg to fit into any of the widely established CompletableFuture holes.
- 在一个用于评估返回的布尔值的主要方法中,我想允许对三种不同方法的调用以异步方式完成.这三种方法都可以返回TRUE或FALSE.
- 如果 any 返回FALSE,我希望评估结果除去其余部分,然后简单地返回该FALSE值.关键是,它可以是三个中的任何,不一定是第一个.
- 但最重要的是,我需要评估,直到所有三个 返回TRUE为止,然后才真正返回TRUE.这是非常重要的.
- Within a primary method that is evaluating returned booleans, I am wanting to allow calls to three different methods to complete asynchronously. Each of these three methods can return either a TRUE or a FALSE.
- If any return a FALSE, I want the evaluation to drop the remainder and simply return that FALSE value. The point being, it can be any of the three, not necessarily the first.
- But most importantly, I need the evaluation to wait until all three return TRUE before actually returning TRUE. This is critically important.
现在,我正在使用基本的&&
链进行此评估:
Right now, I am using a basic &&
chain to make this evaluation:
public boolean evaluateChecks() {
return checkOne().Join() && checkTwo().Join() && checkThree().Join();
}
但是,这仍然按照特定的顺序执行操作-如果 checkThree()
是第一个返回FALSE值的对象,它仍然必须等到前两个提供了它们的值之后才能进行评估,因为&&
掉线的工作原理.
However this still does things in a specific order - if checkThree()
is the first to return a FALSE value, it still has to wait until the prior two have provided their values before it gets evaluated, due to how the &&
fall-through works.
现在,所有这三种方法都返回 CompletableFuture< Boolean>
,但我可以将它们还原回普通方法以在主要方法中运行 CompletableFuture
的方法没有问题.对它们进行评估.
Right now all three methods return CompletableFuture<Boolean>
, but I have no problem reverting these back into normal methods in order to run a CompletableFuture
in the primary method that evaluates them.
我查看了很多示例,但似乎没有一个可以为我提供所需的功能.
I have looked at quite a few examples, but none seem to provide me with the functionality I need.
推荐答案
从Didier L的答案改编而成:
Adapted from the answer by Didier L here:
使用 thenAccept
来完成 CompletableFuture
,而不是使用 exceptionally
和 completeExceptionally
>由 allOf()
返回.
Instead of using exceptionally
and completeExceptionally
, use thenAccept
to complete
the CompletableFuture
returned by allOf()
.
例如,保存期货,因为您要对它们进行连锁操作:
For example, save the futures because you're about to chain actions on them:
CompletableFuture<Boolean> a = checkOne(), b = checkTwo(), c = checkThree();
Use CompletableFuture.allOf
to wait for all of them to complete (presumably you're not expecting failures/exceptions) and transform the Void
result into the expected Boolean
value of true
.
CompletableFuture<Boolean> allWithFailFast = CompletableFuture.allOf(a, b, c).thenApply(__ -> a.join() && b.join() && c.join());
// if there is an exception, it'll be propagated to the future returned by thenApply
有了上面返回的 CompletableFuture
,如果任何原始期货以 false
完成,您现在可以更快地完成 complete
.
With the returned CompletableFuture
above, you can now complete
it faster if any of the original futures completes with false
.
Stream.of(a, b, c).forEach(f -> f.thenAccept(result -> {
if (!result) {
allWithFailFast.complete(false);
}
}));
根据完成顺序及其结果, allOf
期货将首先完成,并带有评估期货的结果,或者其中一个期货将返回 false
,并且导致 allWithFailFast
以 false
完成.
Depending on the order of completions and their result, either the allOf
future will complete first with a result of evaluating the futures or one of the futures will return false
and cause the allWithFailFast
to complete with false
.
如果多个期货都以 false
完成,则只有第一个调用
If multiple futures complete with false
, only the first call to
allWithFailFast.complete(false);
会做任何事情,其他的基本上都会被忽略.
will do anything, the others will essentially be ignored.
这篇关于CompletableFuture如何先返回FALSE或等到全部完成后再返回TRUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!