Command Palette

Search for a command to run...

Back to the lesson: Topic 13.7 — Future and CompletableFuture
Core Java · Example 1 of 4 Java 8+

A pipeline with thenApply, thenCompose and thenCombine

CompletableFuture (Java 8) is a Future you can build pipelines on: start work asynchronously, transform results with thenApply, chain dependent calls with thenCompose, combine independent ones with thenCombine/allOf, and handle failures with exceptionally/handle, without blocking a thread at every step.

Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Start two async tasks that return 6 and 7, multiply their results with thenCombine and print the product.

02

Make a chain that parses "x12" as an int asynchronously and falls back to -1 on failure.

Explain it without notes

01

What can CompletableFuture do that a plain Future can't?

02

Explain the difference between thenApply and thenCompose.

03

How do exceptions propagate through a CompletableFuture chain, and how do you handle them?

A pipeline with thenApply, thenCompose and thenCombine Java 8+
Sign in to run this example in your browser.

Expected output

ASHA pays 70