| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6f8782d commit 56ee0b9
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,29 +20,29 @@ public Thread newThread(Runnable r) { | |||
| 20 | 20 | return t; | |
| 21 | 21 | } | |
| 22 | 22 | }); | |
| 23 | - | ||
| 24 | 23 | /* | |
| 25 | 24 | public List<String> findPriceSequential(String product) { | |
| 26 | 25 | return shops.stream() | |
| 27 | 26 | .map(shop -> shop.getName() + " price is " + shop.calculatePrice(product)) | |
| 28 | - .collect(Collectors.<String>toList()); | ||
| 27 | + .collect(Collectors.toList()); | ||
| 29 | 28 | } | |
| 30 | 29 | ||
| 31 | 30 | public List<String> findPriceParallel(String product) { | |
| 32 | 31 | return shops.parallelStream() | |
| 33 | 32 | .map(shop -> shop.getName() + " price is " + shop.calculatePrice(product)) | |
| 34 | - .collect(Collectors.<String>toList()); | ||
| 33 | + .collect(Collectors.toList()); | ||
| 35 | 34 | } | |
| 36 | 35 | ||
| 37 | 36 | public List<String> findPrice(String product) { | |
| 38 | 37 | List<CompletableFuture<String>> priceFutures = | |
| 39 | 38 | 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()); | ||
| 42 | 42 | ||
| 43 | 43 | List<String> prices = priceFutures.stream() | |
| 44 | 44 | .map(CompletableFuture::join) | |
| 45 | - .collect(Collectors.<String>toList()); | ||
| 45 | + .collect(Collectors.toList()); | ||
| 46 | 46 | return prices; | |
| 47 | 47 | //return sequence(priceFutures).join(); | |
| 48 | 48 | } | |
@@ -52,15 +52,15 @@ public List<String> findPriceSequential(String product) { | |||
| 52 | 52 | .map(shop -> shop.getPrice(product)) | |
| 53 | 53 | .map(Quote::parse) | |
| 54 | 54 | .map(Discount::applyDiscount) | |
| 55 | - .collect(Collectors.<String>toList()); | ||
| 55 | + .collect(Collectors.toList()); | ||
| 56 | 56 | } | |
| 57 | 57 | ||
| 58 | 58 | public List<String> findPriceParallel(String product) { | |
| 59 | 59 | return shops.parallelStream() | |
| 60 | 60 | .map(shop -> shop.getPrice(product)) | |
| 61 | 61 | .map(Quote::parse) | |
| 62 | 62 | .map(Discount::applyDiscount) | |
| 63 | - .collect(Collectors.<String>toList()); | ||
| 63 | + .collect(Collectors.toList()); | ||
| 64 | 64 | } | |
| 65 | 65 | ||
| 66 | 66 | public List<String> findPrice(String product) { | |
@@ -69,7 +69,7 @@ public List<String> findPrice(String product) { | |||
| 69 | 69 | ||
| 70 | 70 | return priceFutures.stream() | |
| 71 | 71 | .map(CompletableFuture::join) | |
| 72 | - .collect(Collectors.<String>toList()); | ||
| 72 | + .collect(Collectors.toList()); | ||
| 73 | 73 | } | |
| 74 | 74 | ||
| 75 | 75 | public Stream<CompletableFuture<String>> findPriceStream(String product) { | |
@@ -85,5 +85,7 @@ public void printPricesStream() { | |||
| 85 | 85 | .map(f -> f.thenAccept(s -> System.out.println(s + " (done in " + ((System.nanoTime() - start) / 1_000_000) + " msecs)"))) | |
| 86 | 86 | .toArray(size -> new CompletableFuture[size]); | |
| 87 | 87 | CompletableFuture.allOf(futures).join(); | |
| 88 | + System.out.println("All shops have now responded in " + ((System.nanoTime() - start) / 1_000_000) + " msecs"); | ||
| 88 | 89 | } | |
| 90 | + | ||
| 89 | 91 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,9 +11,9 @@ public class BestPriceFinderMain { | |||
| 11 | 11 | private static BestPriceFinder bestPriceFinder = new BestPriceFinder(); | |
| 12 | 12 | ||
| 13 | 13 | 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")); | ||
| 17 | 17 | bestPriceFinder.printPricesStream(); | |
| 18 | 18 | } | |
| 19 | 19 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments