Skip to content

Commit 7c82386

Browse files
committed
feat: CompletableFuture in Java (Explained Simply).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 382fdfb commit 7c82386

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

JAVA8/CompletableFuture/src/CompletableFuture in Java (Explained Simply).txt renamed to Java 8 Crash Course/Completable Future/src/CompletableFuture In Java.txt

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Why Use CompletableFuture?
1818
Basic Example:
1919

2020
Let's start with a simple example:
21+
2122
import java.util.concurrent.CompletableFuture;
2223

2324
public class CompletableFutureExample {
@@ -31,44 +32,52 @@ public class CompletableFutureExample {
3132
}
3233
}
3334

34-
Output:
35+
Output:
3536

3637
Running async task in: ForkJoinPool.commonPool-worker-1
3738

38-
Common Use Cases & Methods**
39+
---
40+
41+
Common Use Cases & Methods
3942

40-
1️⃣ Run a Task & Return a Value**
43+
1. Run a Task & Return a Value
4144

4245
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
4346
return "Hello, World!";
4447
});
4548

4649
System.out.println(future.join()); // Output: Hello, World!
4750

48-
🟢 .supplyAsync() runs in the background and returns a value.
51+
.supplyAsync() runs in the background and returns a value.
52+
53+
---
4954

50-
2️⃣ Chaining Tasks (`thenApply()`)
55+
2. Chaining Tasks (thenApply)
5156

52-
If you want to **transform** the result of a future:
57+
If you want to transform the result of a future:
5358

5459
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Java")
5560
.thenApply(name -> "Hello, " + name);
5661

5762
System.out.println(future.join()); // Output: Hello, Java
5863

59-
🟢 .thenApply() is used to process and transform the result.
64+
.thenApply() is used to process and transform the result.
65+
66+
---
6067

61-
3️⃣ Running a Task After Another (`thenRun()`)
68+
3. Running a Task After Another (thenRun)
6269

6370
If the second task doesn’t need the result from the first task:
6471

6572
CompletableFuture.supplyAsync(() -> "Task 1 Completed")
6673
.thenRun(() -> System.out.println("Task 2 Started"))
6774
.join();
6875

69-
✅ The second task runs after the first task but doesn’t take input.
76+
The second task runs after the first task but doesn’t take input.
77+
78+
---
7079

71-
4️⃣ Combining Two `CompletableFuture` (`thenCombine()`)**
80+
4. Combining Two CompletableFutures (thenCombine)
7281

7382
Run two async tasks and combine their results:
7483

@@ -78,9 +87,11 @@ CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World")
7887
CompletableFuture<String> result = future1.thenCombine(future2, (res1, res2) -> res1 + " " + res2);
7988
System.out.println(result.join()); // Output: Hello World
8089

81-
🟢 .thenCombine() takes results from two async tasks and merges them.
90+
.thenCombine() takes results from two async tasks and merges them.
91+
92+
---
8293

83-
5️⃣ Handling Errors (exceptionally())
94+
5. Handling Errors (exceptionally)
8495

8596
Handle errors without crashing the application:
8697

@@ -94,7 +105,9 @@ CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
94105

95106
System.out.println(future.join()); // Output: 0
96107

97-
🟢 .exceptionally() allows fallback values when an exception occurs.
108+
.exceptionally() allows fallback values when an exception occurs.
109+
110+
---
98111

99112
Using CompletableFuture for Parallel Execution
100113

@@ -108,18 +121,22 @@ CompletableFuture<Void> future = CompletableFuture.allOf(
108121

109122
future.join();
110123

111-
✅ All tasks execute in parallel.
124+
All tasks execute in parallel.
125+
126+
---
112127

113128
CompletableFuture vs Future:
114129

115130
| Feature | Future | CompletableFuture |
116131
|---------------------|----------------------------------|--------------------------------------|
117-
| Blocking Call | Yes (needs .get()) | ❌ No (async methods) |
118-
| Chaining Tasks | No | ✅ Yes (thenApply(), thenRun()) |
119-
| Exception Handling | No | ✅ Yes (exceptionally()) |
120-
| Combining Results | No | ✅ Yes (thenCombine(), allOf()) |
132+
| Blocking Call | Yes (needs .get()) | No (async methods) |
133+
| Chaining Tasks | No | Yes (thenApply(), thenRun()) |
134+
| Exception Handling | No | Yes (exceptionally()) |
135+
| Combining Results | No | Yes (thenCombine(), allOf()) |
121136

137+
---
122138

123-
✅ CompletableFuture makes asynchronous programming easy in Java.
124-
✅ It's non-blocking, supports chaining, and parallel execution.
125-
✅ Use it to improve performance in applications with I/O operations, network calls, or database queries.
139+
Summary:
140+
CompletableFuture makes asynchronous programming easy in Java.
141+
It's non-blocking, supports chaining, and parallel execution.
142+
Use it to improve performance in applications with I/O operations, network calls, or database queries.

0 commit comments

Comments
 (0)