-
Notifications
You must be signed in to change notification settings - Fork 0
/
GmoCompletableFutureDemo.java
51 lines (40 loc) · 1.47 KB
/
GmoCompletableFutureDemo.java
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
49
50
51
package challenge1.java8;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
import externalLegacyCodeNotUnderOurControl.PriceService;
public class GmoCompletableFutureDemo {
private PriceService service = new PriceService();
private void run() {
Supplier<Integer> sup_getprice = () -> {
return service.getPrice();
};
CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(sup_getprice);
CompletableFuture<Integer> cf2 = CompletableFuture.supplyAsync(sup_getprice);
CompletableFuture<Integer> cf3 = CompletableFuture.supplyAsync(sup_getprice);
CompletableFuture<?>[] cfa = {cf1, cf2, cf3};
CompletableFuture<Void> alldone = CompletableFuture.allOf(cfa);
//CompletableFuture<Void> alldone = CompletableFuture.allOf(cf1, cf2, cf3);
alldone.thenRun(() -> {
try {
assert cf1.isDone();
assert cf2.isDone();
assert cf3.isDone();
double avg = (cf1.get()+cf2.get()+cf3.get())/3.0;
System.out.println("Average price = "+avg);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
});
try {
System.out.println("Main thread waiting for completion.");
Thread.sleep(5000);
System.out.println("Main thread done.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new GmoCompletableFutureDemo().run();
}
}