Core Java

Java 8 CompletableFuture runAsync Example

Hello. In this tutorial, we will explore the Java 8 CompletableFuture and explain the runAsync method.

1. Introduction

Before diving deep into the practice stuff let us understand the runAsync(…) method we will be covering in this tutorial.

  • Used to run a method asynchronously. The method accepts a Runnable functional interface
  • The method does not return any value
  • Supports the callback chains via thenRun*(…) method
  • Supports the Executor Service by supplying the Executor object as a method argument

2. Java 8 CompletableFuture runAsync Practice

Let us dive into some practice stuff from here and I am assuming that you already have the Java 1.8 or greater installed in your local machine. I am using JetBrains IntelliJ IDEA as my preferred IDE. You’re free to choose the IDE of your choice.

2.1 Understanding runAsync() method

Create a test class in the com.jcg.java8 package and add the following code to it. The class will show the method implementation in three different ways and a simple assertion to confirm the method returns a void.

package com.jcg.java8;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class TestRunAsyncMethod {

  //CompletableFuture.runAsync(...) method is used to run the asynchronous tasks, and it returns a completable future.
  //The method returns a CompletableFuture<Void>.

  void test_runAsync() throws ExecutionException, InterruptedException {
    CompletableFuture<Void> c =
        CompletableFuture.runAsync(() -> System.out.println("task executing in async mode"));


  void test_runAsyncCallbacksChain() throws ExecutionException, InterruptedException {
    CompletableFuture<Void> c =
        CompletableFuture.runAsync(() -> System.out.println("running runAsync(...) method"))
            .thenRunAsync(() -> System.out.println("running callback chain"));


  void test_runAsyncWithExecutorService() throws ExecutionException, InterruptedException {
    ExecutorService pool = Executors.newFixedThreadPool(2);

    CompletableFuture<Void> c =
            () -> System.out.println("running runAsync(...) method with an executor"), pool);



Run the file as a JUnit test and if everything goes well the following output will be logged in the IDE console and all the test cases should pass.

Console output

-- method #2
running runAsync(...) method
running callback chain

-- method #1
task executing in async mode

-- method #3
running runAsync(...) method with an executor

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned runAsync(…) method introduced in java8 programming. CompletableFuture.runAsync(…) method is used to run a Runnable method asynchronously. You can download the source code from the Downloads section.

4. Download the Project

This was a tutorial on learning and implementing the runAsync(…) in java8 programming.

You can download the full source code of this example here: Java 8 CompletableFuture runAsync Example

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions



An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button