There's no public constructor in CompletableFuture
class which accepts a parameter. It's better to use CompletableFuture.supplyAsync()
static method which takes a Supplier
instead of Callable
:
CompletableFuture<A> future = CompletableFuture.supplyAsync(A::new);
Or using lambda:
CompletableFuture<A> future = CompletableFuture.supplyAsync(() -> new A());
Note that if A
constructor throws checked exception, you should handle it as Supplier
interface does not support exceptions:
CompletableFuture<A> future = CompletableFuture.supplyAsync(() -> {
try { return new A(); }
catch(Exception ex) { throw new RuntimeException(ex);}
});
Finally note that supplyAsync
will not only create the CompletableFuture
, but also schedule it for execution in common thread pool. You can specify custom Executor
as second parameter to supplyAsync
method. If you want to create CompletableFuture
without sending it imediately for execution, then probably you don't need a CompletableFuture
.