前提知识

创建线程的几种方式

 1.继承ThreadThread01 thread01 = new Thread01();thread01.start();//启动线程2.实现Runnable接口new Thread(new Runable01()).start();3.实现Callable接口+FutureTask(可以拿到返回结果,可以处理异常)FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());new Thread(futureTask).start();//阻塞等待整个线程执行完成,获取返回结果Integer integer = futureTask.get();4.线程池给线程池提交任务ExecutorService executorService = Executors.newFixedThreadPool(10);executorService.execute(new Runable01());

记住!
生产实践中是使用自定义线程池! 由公司的架构师负责设计好的。

一、配置线程池

在config包下配置

@ConfigurationProperties(prefix = "mall.thread")
@Component
@Data
public class ThreadPoolConfigProperties {private Integer corePoolSize;private Integer maxSize;private Integer keepAliveTime;
}

可以application.yml配置线程池参数

# 线程池的配置
mall:thread:max-size: 200keep-alive-time: 10core-pool-size: 20

将线程池注入容器

@Configuration
public class MyThreadConfig {@Beanpublic ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {return new ThreadPoolExecutor(pool.getCorePoolSize(),pool.getMaxSize(),pool.getKeepAliveTime(),TimeUnit.SECONDS,new LinkedBlockingDeque<>(100000),Executors.defaultThreadFactory(),new ThreadPoolExecutor.DiscardPolicy());}
}

实战任务

  1. sku基本信息获取 pms_sku_info
  2. sku的图片信息 pms_sku_images
  3. 获取spu的销售属性组合
  4. 获取spu的介绍 pms_spu_info_desc
  5. 获取spu的规格参数信息

1和2可以同时异步执行,3,4,5要等1的返回结果。1,2,3,4,5都完成才能返回总结果。

异步实现

CompletableFuture组合式异步编程

runAsync 和 supplyAsync方法

CompletableFuture 提供了四个静态方法来创建一个异步操作。

public static CompletableFuture<Void> runAsync(Runnable runnable)
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)

没有指定Executor的方法会使用ForkJoinPool.commonPool() 作为它的线程池执行异步代码。如果指定线程池,则使用指定的线程池运行。以下所有的方法都类同。

  • runAsync方法不支持返回值。
  • supplyAsync可以支持返回值。

实现代码

  @Overridepublic SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {SkuItemVo skuItemVo = new SkuItemVo();//supplyAsync 有返回值CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {//1.sku基本信息获取 pms_sku_infoSkuInfoEntity info = getById(skuId);skuItemVo.setInfo(info);Long spuId = info.getSpuId();Long catalogId = info.getCatalogId();return info;}, executor);//thenAcceptAsync能接受上一步结果,但是无返回值CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((res) -> {//3.获取spu的销售属性组合List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());skuItemVo.setSaleAttr(saleAttrVos);}, executor);//thenAcceptAsync能接受上一步结果,但是无返回值CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {//4.获取spu的介绍 pms_spu_info_descSpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());skuItemVo.setDesp(spuInfoDescEntity);}, executor);//thenAcceptAsync能接受上一步结果,但是无返回值CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync((res) -> {//5.获取spu的规格参数信息List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());skuItemVo.setGroupAttrs(attrGroupVos);}, executor);//runAsync 无返回值CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {//2.sku的图片信息 pms_sku_imagesList<SkuImagesEntity> images = skuimagesService.getImagesBySkuId(skuId);skuItemVo.setImages(images);}, executor);//等待所有任务都完成CompletableFuture.allOf(saleAttrFuture, descFuture, baseAttrFuture, imageFuture).get();return skuItemVo;}

springboot项目多线程实战之异步编排任务相关推荐

  1. springboot项目开发实战

    文章目录 springboot项目开发实战之后端流程详解 一.创建简单的springboot之web项目 1.简单springboot框架环境搭建 2.相关jar包的导入 3.配置文件yaml的设置 ...

  2. springboot微服务实战:初探异步线程池(四种创建多线程对比)

    四种多线程对比(异步) 创建和初始化多线程的几种方式1.继承Thread2.实现Runnable接口3.实现Callable接口 + FutureTask(可以拿到返回结果,可以处理异常)4.线程池 ...

  3. 【多线程】优雅使用线程池结合CompletableFuture实现异步编排

    文章目录 参考 1.线程池引入 2.Executors 2.1.概述 2.2.Executors缺陷 3.优雅的创建线程池 3.1.正确挑选方法 3.2.线程池配置类 4.线程池执行流程 5.Comp ...

  4. K8S实战基础篇:一文带你深入了解K8S实战部署SpringBoot项目

    K8S实战基础篇:一文带你深入了解K8S实战部署SpringBoot项目 1.前言 2.简介 2.1.为什么写这篇文章 2.2.需求描述 2.3.需求分析 3. 部署实战 3.1 环境准备 3.2 i ...

  5. docker部署项目 dockerfile 实战 SpringBoot、flask

    1 SpringBoot微服务打包Docker镜像 1.1 构建springboot项目 package com.example.springboot.demo;import org.springfr ...

  6. springboot 项目实战 基本框架搭建(IDEA)

    springboot 项目实战 基本框架搭建(IDEA) IDEA下载 我使用的是破解的专业版IDEA,使用权一直到2089年: 下载IDEA: 下载processional版本,然后百度搜索激活码即 ...

  7. 【SpringBoot项目实战+思维导图】瑞吉外卖①(项目介绍、开发环境搭建、后台登陆/退出功能开发)

    文章目录 软件开发整体介绍 软件开发流程 角色分工 软件环境 瑞吉外卖项目介绍 项目介绍 产品原型 技术选型 功能架构 角色 开发环境搭建 数据库环境搭建 创建数据库 数据库表导入 数据库表介绍 Ma ...

  8. Vue整合SpringBoot项目实战之Vue+Element-Ui搭建前端项目

    Vue整合SpringBoot项目实战之Vue+Element-Ui搭建前端项目 源码(欢迎star): 前端项目代码 后端项目代码 系列文章: Vue整合SpringBoot项目实战之后端业务处理 ...

  9. springboot+vue项目大型实战(一)后端开发

    源码下载地址!!!点我 数据库创建表 SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0;-- ---------------------------- -- ...

最新文章

  1. VMware上实现LVS负载均衡(NAT)
  2. C++自动生成的成员函数
  3. Oracle工具类-生成数据库现有Job的创建脚本
  4. WSL安装Oracle,WSL安装JDK8 - terwergreen的个人空间 - OSCHINA - 中文开源技术交流社区...
  5. 软件工程——团队作业3
  6. 【机器学习】特征提取代码汇总
  7. MySql error 1010 无法删除数据库
  8. WIFI vs 无线网
  9. linux 文件系统路径,Linux编程 1 (文件系统路径说明, 目录结构说明)
  10. sqlerror.java 1074_java.sql.SQLException: Before start of result set异常
  11. 81、通过secureCRT连接虚拟机时几种连接方式的不同
  12. R语言数据可视化---交互式图表recharts包
  13. Silverlight 2初尝及我的例子:Fanfou.com客户端——EatSilverlight
  14. ipython 安装_IPYTHON安装.DOC
  15. 热门编程语言间的差异
  16. dojo--inherited
  17. 优化ESD防护的PCB设计准则
  18. ASP.NET Core 项目文件夹解读新框架
  19. C语言之三目运算符---学习笔记
  20. 线性代数:裁剪变换(投影变换)(一)

热门文章

  1. h264 NAL 类型描述
  2. pageOffice支持html5,使用pageoffice进行在线预览
  3. 一级计算机考证多少分过
  4. React 实现计时器
  5. Flex + LCDS + J2EE Web项目(1)
  6. Bmob 移动后端云服务器平台实现登录注册
  7. excel导出图片方格居中
  8. python调用百度AI对颜值评分
  9. python问题两个乒乓球队进行比赛_Python练习题 017:三支乒乓球队出赛名单
  10. 运行IE浏览器产生指令引用内存错误如何解决?