-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcompletablefuture-chaining.yaml
More file actions
48 lines (48 loc) · 1.55 KB
/
completablefuture-chaining.yaml
File metadata and controls
48 lines (48 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
---
id: 50
slug: "completablefuture-chaining"
title: "CompletableFuture chaining"
category: "concurrency"
difficulty: "intermediate"
jdkVersion: "8"
oldLabel: "Pre-Java 8"
modernLabel: "Java 8+"
oldApproach: "Blocking Future.get()"
modernApproach: "CompletableFuture"
oldCode: |-
Future<String> future =
executor.submit(this::fetchData);
String data = future.get(); // blocks
String result = transform(data);
modernCode: |-
CompletableFuture.supplyAsync(
this::fetchData
)
.thenApply(this::transform)
.thenAccept(IO::println);
summary: "Chain async operations without blocking, using CompletableFuture."
explanation: "CompletableFuture enables non-blocking async pipelines. Chain operations\
\ with thenApply, thenCompose, thenAccept. Handle errors with exceptionally(). Combine\
\ multiple futures with allOf/anyOf."
whyModernWins:
- icon: "🔗"
title: "Chainable"
desc: "Compose async steps into a readable pipeline."
- icon: "🚫"
title: "Non-blocking"
desc: "No thread sits idle waiting for results."
- icon: "🛡️"
title: "Error handling"
desc: "exceptionally() and handle() for clean error recovery."
support:
state: "available"
description: "Widely available since JDK 8 (March 2014)"
prev: "concurrency/stable-values"
next: "concurrency/executor-try-with-resources"
related:
- "concurrency/stable-values"
- "concurrency/scoped-values"
- "concurrency/structured-concurrency"
docs:
- title: "CompletableFuture"
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/concurrent/CompletableFuture.html"