11package lambdasinaction .chap10 ;
22
3- import java .util .*;
4- import java .util .concurrent .*;
5- import java .util .stream .*;
3+ import java .util .Arrays ;
4+ import java .util .List ;
5+ import java .util .concurrent .CompletableFuture ;
6+ import java .util .concurrent .Executor ;
7+ import java .util .concurrent .Executors ;
8+ import java .util .concurrent .ThreadFactory ;
9+ import java .util .stream .Collectors ;
10+ import java .util .stream .Stream ;
611
712public class BestPriceFinder {
813
@@ -20,68 +25,42 @@ public Thread newThread(Runnable r) {
2025 return t ;
2126 }
2227 });
23- /*
24- public List<String> findPriceSequential(String product) {
25- return shops.stream()
26- .map(shop -> shop.getName() + " price is " + shop.calculatePrice(product))
27- .collect(Collectors.toList());
28- }
29-
30- public List<String> findPriceParallel(String product) {
31- return shops.parallelStream()
32- .map(shop -> shop.getName() + " price is " + shop.calculatePrice(product))
33- .collect(Collectors.toList());
34- }
35-
36- public List<String> findPrice(String product) {
37- List<CompletableFuture<String>> priceFutures =
38- shops.stream()
39- .map(shop -> CompletableFuture.supplyAsync(() -> shop.getName() + " price is "
40- + shop.calculatePrice(product), executor))
41- .collect(Collectors.toList());
4228
43- List<String> prices = priceFutures.stream()
44- .map(CompletableFuture::join)
45- .collect(Collectors.toList());
46- return prices;
47- //return sequence(priceFutures).join();
48- }
49- /*/
50- public List <String > findPriceSequential (String product ) {
29+ public List <String > findPricesSequential (String product ) {
5130 return shops .stream ()
5231 .map (shop -> shop .getPrice (product ))
5332 .map (Quote ::parse )
5433 .map (Discount ::applyDiscount )
5534 .collect (Collectors .toList ());
5635 }
5736
58- public List <String > findPriceParallel (String product ) {
37+ public List <String > findPricesParallel (String product ) {
5938 return shops .parallelStream ()
6039 .map (shop -> shop .getPrice (product ))
6140 .map (Quote ::parse )
6241 .map (Discount ::applyDiscount )
6342 .collect (Collectors .toList ());
6443 }
6544
66- public List <String > findPrice (String product ) {
67- List <CompletableFuture <String >> priceFutures = findPriceStream (product )
45+ public List <String > findPricesFuture (String product ) {
46+ List <CompletableFuture <String >> priceFutures = findPricesStream (product )
6847 .collect (Collectors .<CompletableFuture <String >>toList ());
6948
7049 return priceFutures .stream ()
7150 .map (CompletableFuture ::join )
7251 .collect (Collectors .toList ());
7352 }
7453
75- public Stream <CompletableFuture <String >> findPriceStream (String product ) {
54+ public Stream <CompletableFuture <String >> findPricesStream (String product ) {
7655 return shops .stream ()
7756 .map (shop -> CompletableFuture .supplyAsync (() -> shop .getPrice (product ), executor ))
7857 .map (future -> future .thenApply (Quote ::parse ))
7958 .map (future -> future .thenCompose (quote -> CompletableFuture .supplyAsync (() -> Discount .applyDiscount (quote ), executor )));
8059 }
8160
82- public void printPricesStream () {
61+ public void printPricesStream (String product ) {
8362 long start = System .nanoTime ();
84- CompletableFuture [] futures = findPriceStream ( "myPhone" )
63+ CompletableFuture [] futures = findPricesStream ( product )
8564 .map (f -> f .thenAccept (s -> System .out .println (s + " (done in " + ((System .nanoTime () - start ) / 1_000_000 ) + " msecs)" )))
8665 .toArray (size -> new CompletableFuture [size ]);
8766 CompletableFuture .allOf (futures ).join ();
0 commit comments