I want a CompletableFuture that only signals the completion (e.g. I don't have a return value).
I can instantiate the CompletableFuture as:
CompletableFuture<Void> future = new CompletableFuture<> ();
But what should I feed into the complete method? For example, I can't do
future.complete(new Void());
CompletableFuture<Void> : The Void tells the user there is no result to be expected.
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. CompletableFuture<Void> cf = CompletableFuture.
You declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value. Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.
What is CompletableFuture? A CompltableFuture is used for asynchronous programming. Asynchronous programming means writing non-blocking code. It runs a task on a separate thread than the main application thread and notifies the main thread about its progress, completion or failure.
As you've noticed, you cannot instantiate a Void
object like this. Since you don't care about the future's value, you could just complete it with null
:
future.complete(null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With