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.
TestRunAsyncMethod.java
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>. @Test void test_runAsync() throws ExecutionException, InterruptedException { CompletableFuture<Void> c = CompletableFuture.runAsync(() -> System.out.println("task executing in async mode")); Assertions.assertNull(c.get()); } @Test void test_runAsyncCallbacksChain() throws ExecutionException, InterruptedException { CompletableFuture<Void> c = CompletableFuture.runAsync(() -> System.out.println("running runAsync(...) method")) .thenRunAsync(() -> System.out.println("running callback chain")); Assertions.assertNull(c.get()); } @Test void test_runAsyncWithExecutorService() throws ExecutionException, InterruptedException { ExecutorService pool = Executors.newFixedThreadPool(2); CompletableFuture<Void> c = CompletableFuture.runAsync( () -> System.out.println("running runAsync(...) method with an executor"), pool); Assertions.assertNull(c.get()); pool.shutdown(); } }
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