Core Java

Java 8 CompletableFuture thenRun Example

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

1. Introduction

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

  • CompletableFuture.thenRun() method does not depend on the previous execution
  • CompletableFuture.thenRun() method does not return anything i.e. CompletableFuture<Void>

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.jcg.java8 package and add the following code to it. The class will simply show the method implementation and print the results on the IDE console.

DemoTest.java

package com.jcg.java8;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

// model dto class.
class Employee {

	private final int id;
	private final String name;

	Employee(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		return "Employee [id=" + getId() + ", name=" + getName() + "]";
	}
}

// CompletableFuture.thenRun() method.
public class AppMain {

	static final List<Employee> EMPLOYEES = new ArrayList<>();

	static {
		EMPLOYEES.add(new Employee(1, "abc"));
		EMPLOYEES.add(new Employee(2, "def"));
		EMPLOYEES.add(new Employee(3, "ghi"));
		EMPLOYEES.add(new Employee(4, "xyz"));
		EMPLOYEES.add(new Employee(5, "pqr"));
	}

	public static void main(String[] args) {
		CompletableFuture.supplyAsync(() -> EMPLOYEES)
		.thenRun(() -> EMPLOYEES.forEach(AppMain::print));	// iterating on list and printing the elements.
	}

	private static void print(final Employee employee) {
		System.out.println(employee.toString());
	}
}

Run the file as a java application and it will be shown the logs in the IDE console.

Logs

Employee [id=1, name=abc]
Employee [id=2, name=def]
Employee [id=3, name=ghi]
Employee [id=4, name=xyz]
Employee [id=5, name=pqr]

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 CompletableFuture thenRun method introduced in java 8. The method is used if we do not want to return anything from the callback function and it does not depend on the previous execution. You can download the source code from the Downloads section.

4. Download the Project

This was a tutorial on learning and implementing the CompletableFuture thenRun method in java 8.

Download
You can download the full source code of this example here: Java 8 CompletableFuture thenRun 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