What is the correct way to create an already-completed CompletableFuture

前端 未结 2 1531
旧时难觅i
旧时难觅i 2021-01-07 18:40

I am using Completable futures in java 8 and I want to write a method that, based on a received parameter, either runs multiple tasks with side effects in parallel and then

相关标签:
2条回答
  • 2021-01-07 18:44

    Since Void can not be instantiated, you can only complete a CompletableFuture<Void> with a null result, which is exactly what you also will get when calling join() on the future returned by allOf() once it has been successfully completed.

    So you can use

    CompletableFuture<Void> cf = CompletableFuture.completedFuture(null);
    

    to get such an already completed future.

    But you can also use

    CompletableFuture<Void> cf = CompletableFuture.allOf();
    

    to denote that there are no jobs the result depends on. The result will be exactly the same.

    0 讨论(0)
  • 2021-01-07 18:55

    pass a null I guess:

    CompletableFuture<Void> done = CompletableFuture.completedFuture(null);
    
    0 讨论(0)
提交回复
热议问题