Java 8 CompletableFuture supplyAsync Example
Hello. In this tutorial, we will explore the Java 8 CompletableFuture and explain the supplyAsync method.
1. Introduction
Before diving deep into the practice stuff let us understand the supplyAsync(…)
method we will be covering in this tutorial.
- Run a
Supplier
functional interface asynchronously. ASupplier
functional interface does not accept anything but returns a value - The
completableFuture.get()
blocks until thecompletableFuture
is completed and return the result - Supports the callback chains via
thenApply*(…)
method - Supports the Executor Service by supplying the
Executor
object as a method argument
2. 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 supplyAsync() 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.
TestSupplyAsync.java
package com.jcg.java8; import static org.junit.jupiter.api.Assertions.assertEquals; import java.time.LocalDate; 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.Test; class TestSupplyAsync { //CompletableFuture.supplyAsync(...) method is used to asynchronously run a Supplier functional interface //The method returns a result. @Test void test_supplyAsync() throws ExecutionException, InterruptedException { CompletableFuture<String> c = CompletableFuture.supplyAsync(() -> "Hello World"); assertEquals("Hello World", c.get()); } @Test void test_supplyAsyncCallbackChain() throws ExecutionException, InterruptedException { final String day = LocalDate.now().getDayOfWeek().name(); CompletableFuture<String> c = CompletableFuture.supplyAsync(() -> day) .thenApplyAsync((str) -> str.concat(" is a good day!")); final String message = day + " is a good day!"; assertEquals(message, c.get()); } @Test void test_supplyAsyncWithExecutorService() throws ExecutionException, InterruptedException { final ExecutorService pool = Executors.newFixedThreadPool(2); CompletableFuture<String> c = CompletableFuture.supplyAsync(() -> "JavaCodeGeeks", pool); assertEquals("JavaCodeGeeks", c.get()); pool.shutdown(); } @Test void test_supplyAsyncCallbackChainWithExecutorService() throws ExecutionException, InterruptedException { final ExecutorService pool = Executors.newFixedThreadPool(2); CompletableFuture<String> c = CompletableFuture.supplyAsync(() -> "JavaCodeGeeks", pool) .thenApplyAsync((str) -> str.concat(" is a good website"), pool); assertEquals("JavaCodeGeeks is a good website", c.get()); pool.shutdown(); } }
Run the file as a JUnit test and if everything goes well the logs (if any) will be shown in the IDE console. In this example, we have skipped the logging part to rely upon as per the Supplier
functional interface implementation. All the test cases should pass. 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 the runAsync method introduced in java 8 programming. CompletableFuture.supplyAsync(…)
method is used to run a Supplier
functional interface asynchronously. You can download the source code from the Downloads section.
4. Download the Project
This was a tutorial about the supplyAsync in Java 8.
You can download the full source code of this example here: Java 8 CompletableFuture supplyAsync Example