1
1
package lambdasinaction .chap10 ;
2
2
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 ;
6
11
7
12
public class BestPriceFinder {
8
13
@@ -20,68 +25,42 @@ public Thread newThread(Runnable r) {
20
25
return t ;
21
26
}
22
27
});
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());
42
28
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 ) {
51
30
return shops .stream ()
52
31
.map (shop -> shop .getPrice (product ))
53
32
.map (Quote ::parse )
54
33
.map (Discount ::applyDiscount )
55
34
.collect (Collectors .toList ());
56
35
}
57
36
58
- public List <String > findPriceParallel (String product ) {
37
+ public List <String > findPricesParallel (String product ) {
59
38
return shops .parallelStream ()
60
39
.map (shop -> shop .getPrice (product ))
61
40
.map (Quote ::parse )
62
41
.map (Discount ::applyDiscount )
63
42
.collect (Collectors .toList ());
64
43
}
65
44
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 )
68
47
.collect (Collectors .<CompletableFuture <String >>toList ());
69
48
70
49
return priceFutures .stream ()
71
50
.map (CompletableFuture ::join )
72
51
.collect (Collectors .toList ());
73
52
}
74
53
75
- public Stream <CompletableFuture <String >> findPriceStream (String product ) {
54
+ public Stream <CompletableFuture <String >> findPricesStream (String product ) {
76
55
return shops .stream ()
77
56
.map (shop -> CompletableFuture .supplyAsync (() -> shop .getPrice (product ), executor ))
78
57
.map (future -> future .thenApply (Quote ::parse ))
79
58
.map (future -> future .thenCompose (quote -> CompletableFuture .supplyAsync (() -> Discount .applyDiscount (quote ), executor )));
80
59
}
81
60
82
- public void printPricesStream () {
61
+ public void printPricesStream (String product ) {
83
62
long start = System .nanoTime ();
84
- CompletableFuture [] futures = findPriceStream ( "myPhone" )
63
+ CompletableFuture [] futures = findPricesStream ( product )
85
64
.map (f -> f .thenAccept (s -> System .out .println (s + " (done in " + ((System .nanoTime () - start ) / 1_000_000 ) + " msecs)" )))
86
65
.toArray (size -> new CompletableFuture [size ]);
87
66
CompletableFuture .allOf (futures ).join ();
0 commit comments