This tutorial is to explain the functionality of ComputableFuture class introduced as Java 8 Concurrency API improvement.
ComputableFuture class implements the Future interface; means can be use for future implementation but with additional logic.
Static methods runAsync and supplyAsync allow us to create a ComputableFuture instance out of Runnable and Supplier functional types accordingly.
runAsync – Returns a new CompletableFuture that is asynchronously completed by a task running in the ForkJoinPool.commonPool() after it runs the given action. It returns the new CompletableFuture.
CompletableFuture<Void> java.util.concurrent.CompletableFuture.runAsync(Runnable runnable)
ComputableFuture.runAsync accepts Runnable functional interface and return a ComputableFuture which doesn’t have value
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.junit.Test;
public class runAsyncDemo {
@Test
public void runAsync() throws InterruptedException, ExecutionException {
CompletableFuture future = CompletableFuture.runAsync(() -> System.out.println("runAsync method doesn not return any value"));
System.out.println(future.get());
}
}
Output
runAsync method does not return any value
null