DEV Community

Code Green
Code Green

Posted on • Edited on

4

Explain Difference between Future and CompletableFuture classes in Java

The Future and CompletableFuture classes in Java both represent asynchronous computations, but they have some differences in terms of functionality and usage.

Future:

  • Syntax:
  Future<ResultType> future = executorService.submit(Callable<ResultType> task);
Enter fullscreen mode Exit fullscreen mode
  • Example:
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<String> future = executor.submit(() -> {
    Thread.sleep(2000); // Simulate a time-consuming task
    return "Hello, from Future!";
});
String result = future.get(); // Blocking call to get the result
System.out.println(result);
executor.shutdown();

Enter fullscreen mode Exit fullscreen mode

CompletableFuture:

  • Syntax:
  CompletableFuture<ResultType> future = CompletableFuture.supplyAsync(Supplier<ResultType> supplier);

Enter fullscreen mode Exit fullscreen mode
  • Example:
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    try {
        Thread.sleep(2000); // Simulate a time-consuming task
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "Hello, from CompletableFuture!";
});
future.thenAccept(result -> System.out.println(result)); // Non-blocking callback

Enter fullscreen mode Exit fullscreen mode
  • Differences:
  1. Completion Handling: Future relies on blocking methods like get() for result retrieval, while CompletableFuture provides non-blocking methods like thenAccept() for completion handling.
  2. Composition: CompletableFuture supports fluent API and allows chaining of multiple asynchronous operations, whereas Future does not.
  3. Explicit Completion: CompletableFuture allows explicit completion via methods like complete() or completeExceptionally(), which can be useful in certain scenarios.

Poll:

Which Java related Questions would you like to read more about?

  • Java 8, 11, 17 feature
  • Streams
  • Spring, Spring Boot
  • Multi Threading
  • Design Patterns

Leave a comment below!


Discover the more Java interview question for experienced developers! YouTube Channel Link

Image of Stellar post

How a Hackathon Project became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Watch the video

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay