CompletableFuture的使用案例
需求
需求说明
- 同一款商品,同时搜索出各大电商的售价
- 输出返回:
出来的结果希望是同款产品的再不同网站的价格清单列表,返回一个list
比如:
mysql in jd price is 110.35
mysql in dangdang price is 109.96
mysql in taobao price is 110.38
- 解决方案
- step by step ,按部就班,查完淘宝,查京东。。。。
- all in ,万箭齐发,一口气多线程异步同时查询。。。。
示例
package com.ygf.mytest.juc; import lombok.AllArgsConstructor; import lombok.Data; import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** * @author ygf * @date 2022/9/25 15:37 */ public class CompletableFutureMailDemo { static List<NetMall> list= Arrays.asList( new NetMall("jd"), new NetMall("dangdang"), new NetMall("taobao") ); /** * step one step一家家查询 * @param list * @param productName * @return */ public static List<String> getPrice(List<NetMall> list ,String productName){ return list.stream() .map(netMall -> String.format(productName+" in %s price is %.2f", netMall.getNetMallName(),netMall.calcPrice(productName))) .collect(Collectors.toList()); }; /** * 万箭齐发,异步多线程。 * @param list * @param productName * @return */ public static List<String> getPriceByCompletableFuture(List<NetMall> list ,String productName){ return list.stream().map(netMall -> CompletableFuture.supplyAsync(()-> String.format(productName+" in %s price is %.2f", netMall.getNetMallName(),netMall.calcPrice(productName)))).collect(Collectors.toList()).stream().map(s->s.join()).collect(Collectors.toList()); } public static void main(String[] args) { long startTime = System.currentTimeMillis(); List<String> prices = getPrice(list, "mysql"); prices.forEach(System.out::println); long endTime = System.currentTimeMillis(); System.out.println("----costTime:"+(endTime-startTime)+"毫秒"); System.out.println("---------------------"); long startTime2 = System.currentTimeMillis(); List<String> mysql = getPriceByCompletableFuture(list, "mysql"); mysql.forEach(System.out::println); long endTime2 = System.currentTimeMillis(); System.out.println("----costTime:"+(endTime2-startTime2)+"毫秒"); } } @Data @AllArgsConstructor class NetMall{ private String netMallName; public double calcPrice(String productName){ try{ try{ TimeUnit.SECONDS.sleep(1);}catch(InterruptedException e) {e.printStackTrace();} }catch (Exception e){ e.printStackTrace(); } return ThreadLocalRandom.current().nextDouble() * 2 +productName.charAt(0); } }
返回结果
"C:\Program Files\Java\jdk1.8.0_102\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.2.2\lib\idea_rt.jar=10389:C:\Program Files\JetBrains\IntelliJ IDEA 2020.2.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_102\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\rt.jar;D:\ideaproject\mytest\target\classes;D:\software\maven_repo\org\springframework\boot\spring-boot-starter-web\2.3.12.RELEASE\spring-boot-starter-web-2.3.12.RELEASE.jar;D:\software\maven_repo\org\springframework\boot\spring-boot-starter\2.3.12.RELEASE\spring-boot-starter-2.3.12.RELEASE.jar;D:\software\maven_repo\org\springframework\boot\spring-boot\2.3.12.RELEASE\spring-boot-2.3.12.RELEASE.jar;D:\software\maven_repo\org\springframework\boot\spring-boot-autoconfigure\2.3.12.RELEASE\spring-boot-autoconfigure-2.3.12.RELEASE.jar;D:\software\maven_repo\org\springframework\boot\spring-boot-starter-logging\2.3.12.RELEASE\spring-boot-starter-logging-2.3.12.RELEASE.jar;D:\software\maven_repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\software\maven_repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\software\maven_repo\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\software\maven_repo\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;D:\software\maven_repo\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\software\maven_repo\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\software\maven_repo\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;D:\software\maven_repo\org\springframework\boot\spring-boot-starter-json\2.3.12.RELEASE\spring-boot-starter-json-2.3.12.RELEASE.jar;D:\software\maven_repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.4\jackson-datatype-jdk8-2.11.4.jar;D:\software\maven_repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.4\jackson-datatype-jsr310-2.11.4.jar;D:\software\maven_repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.4\jackson-module-parameter-names-2.11.4.jar;D:\software\maven_repo\org\springframework\boot\spring-boot-starter-tomcat\2.3.12.RELEASE\spring-boot-starter-tomcat-2.3.12.RELEASE.jar;D:\software\maven_repo\org\apache\tomcat\embed\tomcat-embed-core\9.0.46\tomcat-embed-core-9.0.46.jar;D:\software\maven_repo\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;D:\software\maven_repo\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.46\tomcat-embed-websocket-9.0.46.jar;D:\software\maven_repo\org\springframework\spring-web\5.2.15.RELEASE\spring-web-5.2.15.RELEASE.jar;D:\software\maven_repo\org\springframework\spring-beans\5.2.15.RELEASE\spring-beans-5.2.15.RELEASE.jar;D:\software\maven_repo\org\springframework\spring-webmvc\5.2.15.RELEASE\spring-webmvc-5.2.15.RELEASE.jar;D:\software\maven_repo\org\springframework\spring-aop\5.2.15.RELEASE\spring-aop-5.2.15.RELEASE.jar;D:\software\maven_repo\org\springframework\spring-context\5.2.15.RELEASE\spring-context-5.2.15.RELEASE.jar;D:\software\maven_repo\org\springframework\spring-expression\5.2.15.RELEASE\spring-expression-5.2.15.RELEASE.jar;D:\software\maven_repo\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\software\maven_repo\org\springframework\spring-core\5.2.15.RELEASE\spring-core-5.2.15.RELEASE.jar;D:\software\maven_repo\org\springframework\spring-jcl\5.2.15.RELEASE\spring-jcl-5.2.15.RELEASE.jar;D:\software\maven_repo\com\fasterxml\jackson\core\jackson-core\2.13.3\jackson-core-2.13.3.jar;D:\software\maven_repo\com\fasterxml\jackson\core\jackson-databind\2.13.3\jackson-databind-2.13.3.jar;D:\software\maven_repo\com\fasterxml\jackson\core\jackson-annotations\2.13.3\jackson-annotations-2.13.3.jar;D:\software\maven_repo\com\alibaba\fastjson\1.2.75\fastjson-1.2.75.jar" com.ygf.mytest.juc.CompletableFutureMailDemo mysql in jd price is 110.35 mysql in dangdang price is 109.96 mysql in taobao price is 110.38 ----costTime:3091毫秒 --------------------- mysql in jd price is 110.90 mysql in dangdang price is 110.24 mysql in taobao price is 109.49 ----costTime:1009毫秒