Core Java

Java 8 CompletableFuture thenAccept Example

Hello. In this tutorial, we will explore the Java 8 CompletableFuture thenAccept method.

1. Introduction

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

  • CompletableFuture.thenAccept() takes a Consumer and returns CompletableFuture

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 theAccept() method

Create a test class in the com.java8 package and add the following code to it. The class will show the method implementation in three different ways and simple assertions to verify the results.

DemoTest.java

package com.jcg.java8;

import java.time.LocalDateTime;
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;

// CompletableFuture.thenAccept() takes a Consumer<T> and returns CompletableFuture<Void>.

class DemoTest {

	@Test
	void test_thenAcceptDemo() throws ExecutionException, InterruptedException {
		CompletableFuture<Void> c =
				CompletableFuture.supplyAsync(LocalDateTime::now)
				.thenAccept(event -> {
					System.out.println("Event is at = " + event);
				});

		Assertions.assertNull(c.get());
	}

	@Test
	void test_thenAcceptDemo1() throws ExecutionException, InterruptedException {
		CompletableFuture<Void> c =
				CompletableFuture.supplyAsync(LocalDateTime::now)
				.thenApplyAsync(event -> "Current time is " + event)
				.thenAccept(message -> {
					System.out.println("Hello James. " + message);
				});

		Assertions.assertNull(c.get());
	}

	@Test
	void test_thenAcceptDemo2() throws ExecutionException, InterruptedException {
		final ExecutorService pool = Executors.newFixedThreadPool(2);

		CompletableFuture<Void> c =
				CompletableFuture.supplyAsync(() -> "JavaCodeGeeks", pool)
				.thenAccept(message -> {
					System.out.println("Welcome " + message + ".");
				});

		Assertions.assertNull(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. 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 thenAccept(…) method introduced in java8 programming. The method is used if we do not want to return anything from the callback function and just want to run some piece of code after the completion of the CompletableFuture. You can download the source code from the Downloads section.

4. Download the Project

This was a tutorial on learning and implementing the thenAccept(…) method in java 8 programming.

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

Yatin

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).
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button