Skip to content

Codes #299

@siddharthrgade21-a11y

Description

@siddharthrgade21-a11y

import java.util.concurrent.CompletableFuture;

public class AsyncExample {
public static void main(String[] args) {
CompletableFuture userTask = CompletableFuture.supplyAsync(() -> {
simulateDelay(1000);
return "User: Alex";
});

    CompletableFuture<String> orderTask = CompletableFuture.supplyAsync(() -> {
        simulateDelay(1500);
        return "Order: #5521";
    });

    // Combine both results when they finish
    userTask.thenCombine(orderTask, (user, order) -> user + " | " + order)
            .thenAccept(System.out::println)
            .join(); // Wait for completion
}

private static void simulateDelay(int ms) {
    try { Thread.sleep(ms); } catch (InterruptedException e) {}
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions