CompletableFuture can represent future results of asynchronous computations and supports fluent API for chaining computation steps and combining multiple futures.
Source Code
CompletableFuture.supplyAsync(() -> {
// Simulate a long-running Job
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return "Result of the asynchronous computation";
}).thenApply(result -> {
// Executed in the same thread where the previous computation was done
return "Processed " + result;
}).thenAccept(result -> System.out.println(result));