【JUC】7-CompletableFutrue的應用 大廠案例分析 -電商比價

不会java的菜鸟程序员發表於2024-06-12

1.需求說明

1.1 同一款產品,同時搜尋出同款產品在各大電商平臺的售價;

1.2 同一款產品,同時搜尋出本產品在同一個電商平臺下,各個入駐賣家售價是多少;

2.輸出返回:

出來結果希望是同款產品的在不同地方的價格清單列表,返回一個List<String>

《Mysql》 in jd price is 88.05

《Mysql》 in Dangdang price is 86.11

《Mysql》 in Taobao price is 88.05

3.解決方案,比對同一個商品在各個平臺上的價格,要求獲得一個清單列表

3.1 step by step,按部就班,一個一個查

3.2 all in, 一口氣多執行緒非同步任務同時查詢

實現程式碼:

商城物件封裝,對外暴露查價方法,模擬根據產品查詢價格

 1 class NetMall {
 2     @Getter
 3     private String netMall;
 4 
 5     public NetMall(String netMall) {
 6         this.netMall = netMall;
 7     }
 8 
 9     public double calcPrice(String productName) {
10         return mockQueryPriceFromMall(productName);
11     }
12 
13     private double mockQueryPriceFromMall(String productName) {
14         try {
15             TimeUnit.SECONDS.sleep(1);
16         } catch (InterruptedException e) {
17             e.printStackTrace();
18         }
19         return ThreadLocalRandom.current().nextDouble()*2 + productName.charAt(0);
20     }
21 }

查詢價格實現類

 1 public class CompletableFutrueMallDemo {
 2     static List<NetMall> mallList = Arrays.asList(
 3             new NetMall("jd"),
 4             new NetMall("dangdang"),
 5             new NetMall("taobao")
 6     );
 7 
 8     public static List<String> getPrice(List<NetMall> mallList, String productName) {
 9         return mallList.stream()
10                 .map(netMall -> formatPrice(productName, netMall))
11                 .collect(Collectors.toList());
12     }
13 
14     public static List<String> getPriceByCompletableFutrue(List<NetMall> mallList, String productName) {
15         return mallList.stream()
16                 .map(netMall ->
17                         CompletableFuture.supplyAsync(() -> formatPrice(productName, netMall)))
18                 .collect(Collectors.toList())
19                 .stream()
20                 .map(CompletableFuture::join)
21                 .collect(Collectors.toList());
22     }
23 
24     private static String formatPrice(String productName, NetMall netMall) {
25         return String.format("《%s》 in %s price is %.2f", productName, netMall.getNetMall(), netMall.calcPrice(productName));
26     }
27 
28     public static void main(String[] args) {
29         System.out.println("start to query price step by step");
30         long begin = System.currentTimeMillis();
31         List<String> result = getPrice(mallList, "mysql");
32         long end = System.currentTimeMillis();
33         System.out.println("cost time is " + (end - begin) + "ms");
34 
35         System.out.println("=================================");
36 
37         System.out.println("start to query price step by CompletableFutrue");
38         begin = System.currentTimeMillis();
39         result = getPriceByCompletableFutrue(mallList, "mysql");
40         end = System.currentTimeMillis();
41         System.out.println("cost time is " + (end - begin) + "ms");
42 
43         System.out.println("===========result show===========");
44         for (String s : result) {
45             System.out.println(s);
46         }
47     }
48 }

相關文章