WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Concurrency · Intermediate · question 34 of 100

What is a CompletableFuture in Java and how is it used for concurrency?

📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

In Java, CompletableFuture is a class that provides a way to perform asynchronous, non-blocking operations. It can be used to perform a task in the background while the main thread continues executing other tasks.

CompletableFuture is a powerful and flexible tool for concurrency in Java, and it offers several advantages over traditional multi-threading techniques:

It allows for easier composition and chaining of multiple asynchronous tasks It provides a wide range of methods for handling the completion of tasks, such as callbacks and exception handling It supports cancellation of tasks

Here’s an example of how to use CompletableFuture in Java:

// Create a completable future that returns a string
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    // Perform some long-running task
    return "Result of the long-running task";
});

// Attach a callback that prints the result of the task when it completes
future.thenAccept(result -> System.out.println("Result: " + result));

// Do some other work while the task is running in the background
System.out.println("Doing some other work...");

// Wait for the task to complete and print the result
System.out.println(future.get());

In this example, a CompletableFuture is created using the supplyAsync() method, which takes a Supplier function that performs the long-running task. The thenAccept() method is used to attach a callback that prints the result of the task when it completes. The get() method is used to wait for the task to complete and print the result.

CompletableFuture can also be used to compose multiple asynchronous tasks together. For example, the thenCompose() method can be used to chain a series of tasks together, where the result of one task is used as input to the next task.

Here’s an example of how to use thenCompose() in Java:

// Create a completable future that returns a string
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
    // Perform some long-running task
    return "Result of the long-running task";
});

// Create a completable future that transforms the result of the first future
CompletableFuture<Integer> future2 = future1.thenApply(result -> result.length());

// Attach a callback that prints the result of the second future when it completes
future2.thenAccept(result -> System.out.println("Result: " + result));

// Wait for the second future to complete and print the result
System.out.println(future2.get());

In this example, two CompletableFutures are created: one that performs a long-running task and returns a string, and one that transforms the result of the first future by computing its length. The thenAccept() method is used to attach a callback that prints the result of the second future when it completes. The get() method is used to wait for the second future to complete and print the result.

Overall, CompletableFuture is a powerful and flexible tool for concurrency in Java, and it provides a wide range of features for composing and handling asynchronous tasks. By using CompletableFuture, you can simplify your concurrency code and improve the performance of your application.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Concurrency interview — then scores it.
📞 Practice Java Concurrency — free 15 min
📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

All 100 Java Concurrency questions · All topics