CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() ->{
//1.sku基本信息 pms_sku_info
SkuInfoEntity skuInfo = getById(skuId);
skuItemVO.setSkuInfo(skuInfo);
return skuInfo;
},executor);
CompletableFuture<Void> saleAttrFuture = skuInfoFuture.thenAcceptAsync((res) ->{
//3.spu的销售属性组合
List<SkuSaleAttrVO> saleAttrs = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
skuItemVO.setSaleAttrs(saleAttrs);
},executor);
CompletableFuture<Void> descFuture = skuInfoFuture.thenAcceptAsync((res) ->{
//4.spu商品介绍
SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
skuItemVO.setSpuDesc(spuInfoDescEntity);
},executor);
CompletableFuture<Void> baseAttrFuture = skuInfoFuture.thenAcceptAsync((res) ->{
//5.spu规格参数
List<SpuAttrGroupVO> groupAttrs = attrGroupService.getAttrGroupWithAttysBySpuId(
res.getCatalogId(),res.getSpuId()
);
skuItemVO.setGroupAttrs(groupAttrs);
},executor);
CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() ->{
//2.sku图片信息 pms_sku_images
List<SkuImagesEntity> images = skuImagesService.getImagesBySkuId(skuId);
skuItemVO.setImages(images);
},executor);
try {
//等待所有任务执行完成
CompletableFuture.allOf(saleAttrFuture,descFuture,baseAttrFuture,imageFuture).get();
}catch (InterruptedException e){
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
多线程防忘记代码
于 2022-04-09 23:45:31 首次发布