Java CompletableFuture runAsync() Method

The CompletableFuture class in Java provides the runAsync() method to run a task asynchronously without returning a result.

Introduction

The CompletableFuture.runAsync() method is used to run a task asynchronously and return a CompletableFuture that will be completed when the task is finished. This is useful in asynchronous programming when you need to perform non-blocking operations that do not return a result.

runAsync Method Syntax

The syntax for the runAsync method is as follows:

public static CompletableFuture<Void> runAsync(Runnable runnable)
  • The method takes a single parameter runnable of type Runnable, which represents the task to run.
  • The method returns a CompletableFuture<Void> that will be completed when the task is finished.

Examples

Example 1: Logging a Message

In a web application, you might want to log a message asynchronously to avoid blocking the main thread.

import java.util.concurrent.CompletableFuture;

public class LogMessageExample {
    public static void main(String[] args) {
        // Log a message asynchronously
        CompletableFuture<Void> logFuture = CompletableFuture.runAsync(() -> {
            // Simulate delay
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Logged message: Task completed successfully");
        });

        // Wait for the logging to complete
        logFuture.join();
    }
}

Output:

Logged message: Task completed successfully

Example 2: Updating a Status

In a web application, you might want to update a status asynchronously without waiting for the update to complete.

import java.util.concurrent.CompletableFuture;

public class UpdateStatusExample {
    public static void main(String[] args) {
        // Update status asynchronously
        CompletableFuture<Void> statusFuture = CompletableFuture.runAsync(() -> {
            // Simulate status update
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Status updated to: Completed");
        });

        // Continue with other tasks while status is being updated
        System.out.println("Continuing with other tasks...");

        // Wait for the status update to complete
        statusFuture.join();
    }
}

Output:

Continuing with other tasks...
Status updated to: Completed

Example 3: Task Management System

In a task management system, you might want to archive a completed task asynchronously to improve performance.

import java.util.concurrent.CompletableFuture;

public class TaskManagementSystem {
    public static void main(String[] args) {
        // Archive a task asynchronously
        CompletableFuture<Void> archiveFuture = CompletableFuture.runAsync(() -> {
            // Simulate task archiving
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Task archived successfully");
        });

        // Continue with other tasks while archiving is in progress
        System.out.println("Processing other tasks...");

        // Wait for the task archiving to complete
        archiveFuture.join();
    }
}

Output:

Processing other tasks...
Task archived successfully

Conclusion

The CompletableFuture.runAsync() method in Java is used for running tasks asynchronously that do not return a result. It is particularly useful in scenarios where non-blocking operations are required, such as logging messages, updating statuses, or archiving tasks in a task management system.

Comments