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.

Want to be a Java 8 Ninja ?
Subscribe to our newsletter and download the Java 8 Features Ultimate Guide right now!
In order to get you up to speed with the major Java 8 release, we have compiled a kick-ass guide with all the new features and goodies! Besides studying them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

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.

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) { = id; = name;
	public int getId() {
		return id;
	public String getName() {
		return name;
	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) {

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


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.

You can download the full source code of this example here: Java 8 CompletableFuture thenRun Example
Exit mobile version