runAsync and supplyAsync in ComputableFuture in Java8

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 {
      public void runAsync() throws InterruptedException, ExecutionException {
           CompletableFuture future = CompletableFuture.runAsync(() -> System.out.println("runAsync method doesn not return any value"));

runAsync method does not return any value


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s