繁体   English   中英

CompletionStage,CompletableFuture Void - 返回什么?

[英]CompletionStage, CompletableFuture Void - what to return?

这是我的方法:

public CompletionStage<Void> insert(List<HashAction> hashActionList) {
    if(!hashActionList.isEmpty()) {
        return ...
    }
    // what to return here ?
}

如果我的列表为空,我不知道要返回什么。 不确定 null 是否好,因为之后我必须检查 null。

我试过

return CompletableFuture.completedFuture(null);

但我并没有真正相信,因为我随机选择了 CompletionStage 的一个实现

您只需使用空的异步运行方法即可返回CompletableFuture<Void>

public CompletionStage<Void> insert(List<String> hashActionList) {
    if(!hashActionList.isEmpty()) {
        return null;
    }
    return CompletableFuture.runAsync(()->{});
}

或者您可以使用thenAccept返回CompletionStage<Void>并避免 null

return CompletableFuture.completedFuture(null).thenAccept(i->{});

暂无
暂无

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

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