forked from hellokaton/learn-java8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompletableFuture10.java
More file actions
42 lines (38 loc) · 1.17 KB
/
Copy pathCompletableFuture10.java
File metadata and controls
42 lines (38 loc) · 1.17 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
package io.github.biezhi.java8.completablefuture;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
* @author biezhi
* @date 2018/3/25
*/
public class CompletableFuture10 {
private static CompletableFuture<Integer> m1(){
return CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 2333;
});
}
private static CompletableFuture<Integer> m2(){
return CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 8877;
});
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
long start = System.currentTimeMillis();
CompletableFuture.anyOf(m1(), m2())
.thenRun(() -> {
System.out.println(System.currentTimeMillis() - start);
}).get()
;
}
}