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

Core Java · Advanced · question 50 of 100

What is a CompletableFuture in Java? How is it used?

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

CompletableFuture is a class introduced in Java 8 that represents an asynchronous computation that may or may not have completed yet. It is a powerful feature that allows us to write non-blocking code in a much simpler and more intuitive way compared to traditional callback-based approaches.

A CompletableFuture can be used to perform some long-running task asynchronously in the background, while the main thread continues to execute other tasks. It provides a clean and simple way to chain together a series of asynchronous operations, with the ability to handle errors and exceptions in a straightforward manner.

Here is an example of how to use CompletableFuture in Java:

import java.util.concurrent.CompletableFuture;

public class CompletableFutureExample {
    public static void main(String[] args) {
        CompletableFuture<String> completableFuture = new CompletableFuture<>();
    
        new Thread(() -> {
            try {
                Thread.sleep(5000);
                completableFuture.complete("Hello, world!");
            } catch (InterruptedException e) {
                completableFuture.completeExceptionally(e);
            }
        }).start();
    
        completableFuture.thenAccept(message -> System.out.println(message));
    
        System.out.println("Waiting for completableFuture to complete...");
    }
}

In this example, we create a new CompletableFuture object and start a new thread that performs some long-running task (in this case, sleeping for 5 seconds). When the task is complete, we use the complete method to set the result of the CompletableFuture to a string value.

Next, we chain together a thenAccept method call that is called when the CompletableFuture is complete. This method takes a consumer function that is called with the result of the CompletableFuture (in this case, the string "Hello, world!"). We simply print the message to the console.

Finally, we print a message indicating that we are waiting for the CompletableFuture to complete. This message will be printed before the thenAccept method is called, since the CompletableFuture runs asynchronously in the background.

This is just a simple example, but CompletableFuture can be used to perform much more complex asynchronous operations. It provides a powerful and flexible way to handle concurrency and parallelism in Java applications.

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

All 100 Core Java questions · All topics