CompletionStage,CompletableFuture无效-返回什么?

这是我的方法:

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

我不知道如果我的清单为空会返回什么。不确定空值是否好,因为之后我将不得不检查空值。

我尝试过

return CompletableFuture.completedFuture(null);

但是我并不确定,因为我随机选择了一种CompletionStage实现

dagula6 回答:CompletionStage,CompletableFuture无效-返回什么?

您只需使用空的异步运行方法即可返回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->{});
本文链接:https://www.f2er.com/3027390.html

大家都在问