Skip to content

Commit 56ee0b9

Browse files
committed
fix CompletableFuture examples
1 parent 6f8782d commit 56ee0b9

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/main/java/lambdasinaction/chap10/BestPriceFinder.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,29 @@ public Thread newThread(Runnable r) {
2020
return t;
2121
}
2222
});
23-
2423
/*
2524
public List<String> findPriceSequential(String product) {
2625
return shops.stream()
2726
.map(shop -> shop.getName() + " price is " + shop.calculatePrice(product))
28-
.collect(Collectors.<String>toList());
27+
.collect(Collectors.toList());
2928
}
3029
3130
public List<String> findPriceParallel(String product) {
3231
return shops.parallelStream()
3332
.map(shop -> shop.getName() + " price is " + shop.calculatePrice(product))
34-
.collect(Collectors.<String>toList());
33+
.collect(Collectors.toList());
3534
}
3635
3736
public List<String> findPrice(String product) {
3837
List<CompletableFuture<String>> priceFutures =
3938
shops.stream()
40-
.map(shop -> CompletableFuture.supplyAsync(() -> shop.getName() + " price is " + shop.calculatePrice(product)))
41-
.collect(Collectors.<CompletableFuture<String>>toList());
39+
.map(shop -> CompletableFuture.supplyAsync(() -> shop.getName() + " price is "
40+
+ shop.calculatePrice(product), executor))
41+
.collect(Collectors.toList());
4242
4343
List<String> prices = priceFutures.stream()
4444
.map(CompletableFuture::join)
45-
.collect(Collectors.<String>toList());
45+
.collect(Collectors.toList());
4646
return prices;
4747
//return sequence(priceFutures).join();
4848
}
@@ -52,15 +52,15 @@ public List<String> findPriceSequential(String product) {
5252
.map(shop -> shop.getPrice(product))
5353
.map(Quote::parse)
5454
.map(Discount::applyDiscount)
55-
.collect(Collectors.<String>toList());
55+
.collect(Collectors.toList());
5656
}
5757

5858
public List<String> findPriceParallel(String product) {
5959
return shops.parallelStream()
6060
.map(shop -> shop.getPrice(product))
6161
.map(Quote::parse)
6262
.map(Discount::applyDiscount)
63-
.collect(Collectors.<String>toList());
63+
.collect(Collectors.toList());
6464
}
6565

6666
public List<String> findPrice(String product) {
@@ -69,7 +69,7 @@ public List<String> findPrice(String product) {
6969

7070
return priceFutures.stream()
7171
.map(CompletableFuture::join)
72-
.collect(Collectors.<String>toList());
72+
.collect(Collectors.toList());
7373
}
7474

7575
public Stream<CompletableFuture<String>> findPriceStream(String product) {
@@ -85,5 +85,7 @@ public void printPricesStream() {
8585
.map(f -> f.thenAccept(s -> System.out.println(s + " (done in " + ((System.nanoTime() - start) / 1_000_000) + " msecs)")))
8686
.toArray(size -> new CompletableFuture[size]);
8787
CompletableFuture.allOf(futures).join();
88+
System.out.println("All shops have now responded in " + ((System.nanoTime() - start) / 1_000_000) + " msecs");
8889
}
90+
8991
}

src/main/java/lambdasinaction/chap10/BestPriceFinderMain.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class BestPriceFinderMain {
1111
private static BestPriceFinder bestPriceFinder = new BestPriceFinder();
1212

1313
public static void main(String[] args) {
14-
//execute("sequential", () -> bestPriceFinder.findPriceSequential("myPhone"));
15-
//execute("parallel", () -> bestPriceFinder.findPriceParallel("myPhone"));
16-
//execute("composed CompletableFuture", () -> bestPriceFinder.findPrice("myPhone"));
14+
execute("sequential", () -> bestPriceFinder.findPriceSequential("myPhone"));
15+
execute("parallel", () -> bestPriceFinder.findPriceParallel("myPhone"));
16+
execute("composed CompletableFuture", () -> bestPriceFinder.findPrice("myPhone"));
1717
bestPriceFinder.printPricesStream();
1818
}
1919

0 commit comments

Comments
 (0)