How to complete a CompletableFuture<Void>?

Xinchao picture Xinchao · Dec 20, 2017 · Viewed 7.5k times · Source

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());

Answer

Mureinik picture Mureinik · Dec 20, 2017

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);