CompletableFuture runs work off-thread and lets you chain transformations.
CompletableFuture<Integer> future = CompletableFuture
.supplyAsync(() -> 21)
.thenApply(n -> n * 2);
System.out.println("result = " + future.join());
result = 42
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-26