Posts

Showing posts with the label parallel

Java's CompletableFuture reference

 Since the addition of CompletableFuture in Java 8, running background task or parallel processing or multi-threading got really simplified. These are some of the examples on how to use CompletableFuture in standalone java or with Spring Boot to perform background tasks. Complete list of methods available in CompletableFuture can be found here at https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/concurrent/CompletableFuture.html Basic example to call a method in background and return result The task gets executed in Java's default ForkJoin.commonPool() CompletableFuture < String > completableFuture = CompletableFuture .supplyAsync(() -> "Hello" ); String result = completableFuture . join (); System . out . println ( "result = " + result ); Basic example to call a method in background and without returning a result The task gets executed in Java's default ForkJoin.commonPool() CompletableFuture < Void > completableFuture ...