File tree Expand file tree Collapse file tree 2 files changed +9
-11
lines changed
src/main/java/lambdasinaction/chap10 Expand file tree Collapse file tree 2 files changed +9
-11
lines changed Original file line number Diff line number Diff line change @@ -78,4 +78,12 @@ public Stream<CompletableFuture<String>> findPriceStream(String product) {
7878 .map (future -> future .thenApply (Quote ::parse ))
7979 .map (future -> future .thenCompose (quote -> CompletableFuture .supplyAsync (() -> Discount .applyDiscount (quote ), executor )));
8080 }
81+
82+ public void printPricesStream () {
83+ long start = System .nanoTime ();
84+ CompletableFuture [] futures = findPriceStream ("myPhone" )
85+ .map (f -> f .thenAccept (s -> System .out .println (s + " (done in " + ((System .nanoTime () - start ) / 1_000_000 ) + " msecs)" )))
86+ .toArray (size -> new CompletableFuture [size ]);
87+ CompletableFuture .allOf (futures ).join ();
88+ }
8189}
Original file line number Diff line number Diff line change @@ -14,17 +14,7 @@ public static void main(String[] args) {
1414 //execute("sequential", () -> bestPriceFinder.findPriceSequential("myPhone"));
1515 //execute("parallel", () -> bestPriceFinder.findPriceParallel("myPhone"));
1616 //execute("composed CompletableFuture", () -> bestPriceFinder.findPrice("myPhone"));
17- printPricesStream ();
18- }
19-
20- private static void printPricesStream () {
21- long start = System .nanoTime ();
22- List <CompletableFuture <Void >> futures =
23- bestPriceFinder .findPriceStream ("myPhone" )
24- .map (f -> f .thenAccept (s -> System .out .println (s + " (find in " + ((System .nanoTime () - start ) / 1_000_000 ) + " msecs)" )))
25- .collect (toList ());
26-
27- CompletableFuture .allOf (futures .toArray (new CompletableFuture [futures .size ()])).join ();
17+ bestPriceFinder .printPricesStream ();
2818 }
2919
3020 private static void execute (String msg , Supplier <List <String >> s ) {
You can’t perform that action at this time.
0 commit comments