Recovering from failure
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.
Start two async tasks that return 6 and 7, multiply their results with thenCombine and print the product.
Make a chain that parses "x12" as an int asynchronously and falls back to -1 on failure.
Explain it without notes
What can CompletableFuture do that a plain Future can't?
Explain the difference between thenApply and thenCompose.
How do exceptions propagate through a CompletableFuture chain, and how do you handle them?
Expected output
fallback because: inventory service down
stock shown: 0
status: failed