void
,另一个返回CompletionStage<Void>
?void
或CompletionStage<Void>
中的一个相对于另一个有什么优势?x
中的变量thenCompose
。有没有更好的书写方式?boolean
中返回doValidation
并检查true
函数中的performTask
是否是更好的做法?代码示例1:
public CompletionStage<Response> performTask(Request request) throws MyException {
doValidation(request);
return service.performTask(request).thenCompose(serviceResponse -> CompletableFuture.completedFuture(buildMyResponse(serviceResponse)));
}
private void doValidation(Request request) throws MyException {
CompletableFuture.runAsync(() -> {
// Validate
if (*someCondition*) {
throw new MyException(.....);
}
});
}
代码示例2:
public CompletionStage<Response> performTask(Request request) throws MyException {
return doValidation(request).thenCompose(x -> {
return service.performTask(request).thenCompose(serviceResponse -> CompletableFuture.completedFuture(buildMyResponse(serviceResponse)));
});
}
private CompletionStage<Void> doValidation(Request request) throws MyException {
return CompletableFuture.runAsync(() -> {
// Validate
if (*someCondition*) {
throw new MyException(.....);
}
});
}
更具体地说,在这两段代码中有什么区别,一个返回void,另一个返回CompletionStage?使用void或...
What advantages of using one of void or CompletionStage<Void> over the other?
void
后,您将无法执行链接到CompletableFuture
的任何操作。