为什么在CompletableFuture中定义allOf或an

为什么在CompletableFuture中定义allOf或an

本文介绍了CompletionStage:为什么在CompletableFuture中定义allOf或anyOf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用,我很好奇为什么在在那里定义。

I have a framework that uses the interface CompletionStage and I'm curious why the helper methods anyOf or allOf found in CompletableFuture are defined there.

看起来它们应该在接口而不是实现上运行?

Seems like they should be operating on the interfaces rather than the implementation ?

到目前为止,我对CompletionStage接口非常不满意。
是否还有其他兼容CompletionStage的Java库,但有人建议使用不同的超集界面?

I'm quite dissatisfied with the CompletionStage interface thus far.Are there other Java libraries that are CompletionStage compliant but a different superset interface someone can recommend?

或者也许某些库使用其他帮助方法编写,以便与CompletionStage一起使用?

Or perhaps some library written with additional helper methods for working with CompletionStage ?

推荐答案

如果你想要的话,是一种方法提供相同的 anyOf allOf 类型为 CompletionStage 的对象的功能,您可以简单地求助于:

If all you want, is a method providing the same anyOf and allOf functionality for objects of the type CompletionStage, you can simply resort to toCompletableFuture:

public static CompletionStage<Object> anyOf(CompletionStage<?>... css) {
    return CompletableFuture.anyOf(Arrays.stream(css)
        .map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new));
}
public static CompletionStage<Void> allOf(CompletionStage<?>... css) {
    return CompletableFuture.allOf(Arrays.stream(css)
        .map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new));
}

这篇关于CompletionStage:为什么在CompletableFuture中定义allOf或anyOf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 20:33