forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompletableFutureDemo.java
More file actions
29 lines (24 loc) · 1.05 KB
/
Copy pathCompletableFutureDemo.java
File metadata and controls
29 lines (24 loc) · 1.05 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
package com.learnjava.concurent;
import java.util.concurrent.CompletableFuture;
public class CompletableFutureDemo {
public static void main(String[] args) throws Exception {
CompletableFutureDemo completableFutureDemo = new CompletableFutureDemo();
// completableFutureDemo.test_completed_future();
completableFutureDemo.test_completed_thenApply();
}
public void test_completed_future() throws Exception {
String expectedValue = "the expected value";
CompletableFuture<String> alreadyCompleted = CompletableFuture.completedFuture(expectedValue);
System.out.println(alreadyCompleted.get());
}
public void test_completed_thenApply() {
CompletableFuture<Integer> source = new CompletableFuture<>();
CompletableFuture<Integer> next =
source.thenApply(x -> {
System.out.println(Thread.currentThread().getName());
return x + 1;
});
source.complete(10);
System.out.println(next.join()); // 11
}
}