Jan 24, 2022
We use Java CompletableFuture to run tasks asynchronously using supplyAsync().
The task will run asynchronously in the ForkJoinPool.commonPool().
Using this approach we don't need to manage Threads manually.
// Using Lambda Expression
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return "Result of the asynchronous computation";
});