返回void与返回CompletionStage ?]之间是否有任何区别? 更具体地说,这两个代码有什么区别,一个返回void,另一个返回CompletionStage<Void>? [使用void或CompletionStage<Void>中的一个相对于另一个有什么优势? 在代码示例2中,我没有使用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? 要了解差异,您应该尝试记录流程。它将帮助您确定两者的区别。 这里是一个与您采用的方法相似的示例。我提出了自己的示例,因为我不知道您的代码。 public class Test { public static void main(String[] args) { tryWithVoid(); // tryWithCompletionStageVoid(); } public static void tryWithVoid() { System.out.println("Started"); validate(null); System.out.println("Exit"); } public static void tryWithCompletionStageVoid() { System.out.println("Started"); validateCS(null).thenRun(() -> System.out.println("Validation complete")); System.out.println("Exit"); } public static CompletionStage<Void> validateCS(String val) { return CompletableFuture.runAsync(() -> { if (val == null) { System.out.println("Validation failed! value is null"); } }); } public static void validate(String val) { CompletableFuture.runAsync(() -> { if (val == null) { System.out.println("Validation failed! value is null"); } }); } } 如果运行tryWithVoid(),您将看到输出为: Started Exit Validation failed! value is null 而运行tryWithCompletionStageVoid()时将是: Started Validation failed! value is null Validation complete Exit [第一种方法,tryWithVoid(),它不等待操作完成。因此,该操作的结果可能适用于调用之后的代码,也可能不适用。 但是,在第二种方法tryWithCompletionStageVoid()中,它在运行下一阶段之前等待该阶段完成。 如果您在各个阶段之间没有依赖性,则可以使用void 区别在于,返回void后,您将无法执行链接到CompletableFuture的任何操作。 返回CompletableFuture<Void>时,您可以将其他内容链接到它。例如,根据需要调用thenRun(),thenRunAsync()或仅调用join()以等待其完成。 因此,取决于调用者是否关心CompletableFuture的状态,并希望应用附加的逻辑。 在您的情况下,在示例1中,doValidation()在单独的线程上运行,发生的任何事情实际上都是无关紧要的。 service.performTask()将独立于其发生,甚至可以在doValidation()之前开始和/或结束。 在示例2中,doValidation()必须成功完成,然后才能实际发生performTask()。因此,似乎该版本实际上是正确的版本。请记住,正在运行CompletionStage<Response> performTask()的 main 线程将完成并在设置管道后立即返回,实际的performValidation()可能仍在运行(甚至可能尚未启动)。 通过这种方式可以简化呼叫,如下所示: service.performTask(request).thenApply(this::buildMyResponse); 您不必再次将其包装在CompletableFuture中,只需用thenCompose()再次对其进行包装。

问题描述 投票:0回答:2
更具体地说,这两个代码有什么区别,一个返回void,另一个返回CompletionStage<Void>

    [使用voidCompletionStage<Void>中的一个相对于另一个有什么优势?
  • 在代码示例2中,我没有使用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或...

java asynchronous completable-future
2个回答
2
投票
我正在解决您的第一个问题

What advantages of using one of void or CompletionStage<Void> over the other?


2
投票
区别在于,返回void后,您将无法执行链接到CompletableFuture的任何操作。
© www.soinside.com 2019 - 2024. All rights reserved.