|
| 1 | +package com.brianway.learning.java8.effective.async; |
| 2 | + |
| 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; |
| 11 | + |
| 12 | +/** |
| 13 | + * 添加折扣后的版本 |
| 14 | + * 构造同步和异步操作 |
| 15 | + */ |
| 16 | +public class BestPriceFinder { |
| 17 | + |
| 18 | + private final List<Shop> shops = Arrays.asList(new Shop("BestPrice"), |
| 19 | + new Shop("LetsSaveBig"), |
| 20 | + new Shop("MyFavoriteShop"), |
| 21 | + new Shop("BuyItAll"), |
| 22 | + new Shop("ShopEasy")); |
| 23 | + |
| 24 | + private final Executor executor = Executors.newFixedThreadPool(shops.size(), new ThreadFactory() { |
| 25 | + @Override |
| 26 | + public Thread newThread(Runnable r) { |
| 27 | + Thread t = new Thread(r); |
| 28 | + t.setDaemon(true); |
| 29 | + return t; |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + public List<String> findPricesSequential(String product) { |
| 34 | + return shops.stream() |
| 35 | + .map(shop -> shop.getPrice(product)) |
| 36 | + .map(Quote::parse) |
| 37 | + .map(Discount::applyDiscount) |
| 38 | + .collect(Collectors.toList()); |
| 39 | + } |
| 40 | + |
| 41 | + public List<String> findPricesParallel(String product) { |
| 42 | + return shops.parallelStream() |
| 43 | + .map(shop -> shop.getPrice(product)) |
| 44 | + .map(Quote::parse) |
| 45 | + .map(Discount::applyDiscount) |
| 46 | + .collect(Collectors.toList()); |
| 47 | + } |
| 48 | + |
| 49 | + public List<String> findPricesFuture(String product) { |
| 50 | + List<CompletableFuture<String>> priceFutures = findPricesStream(product) |
| 51 | + .collect(Collectors.toList()); |
| 52 | + |
| 53 | + return priceFutures.stream() |
| 54 | + .map(CompletableFuture::join) |
| 55 | + .collect(Collectors.toList()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * 分了三步: |
| 60 | + * 1. 获取价格 |
| 61 | + * 2. 解析报价 |
| 62 | + * 3. 为计算折扣价格构造 Future |
| 63 | + */ |
| 64 | + public Stream<CompletableFuture<String>> findPricesStream(String product) { |
| 65 | + return shops.stream() |
| 66 | + .map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice(product), executor)) |
| 67 | + .map(future -> future.thenApply(Quote::parse)) |
| 68 | + .map(future -> future.thenCompose(quote -> CompletableFuture.supplyAsync( |
| 69 | + () -> Discount.applyDiscount(quote), executor))); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * 响应 CompletableFuture 的 completion 事件 |
| 74 | + * 把 Util 类中的 delay 调一下效果更明显 |
| 75 | + */ |
| 76 | + public void printPricesStream(String product) { |
| 77 | + long start = System.nanoTime(); |
| 78 | + CompletableFuture[] futures = findPricesStream(product) |
| 79 | + .map(f -> f.thenAccept(s -> System.out.println(s + " (done in " + ((System.nanoTime() - start) / 1_000_000) + " msecs)"))) |
| 80 | + .toArray(CompletableFuture[]::new); |
| 81 | + CompletableFuture.allOf(futures).join(); |
| 82 | + System.out.println("All shops have now responded in " + ((System.nanoTime() - start) / 1_000_000) + " msecs"); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments