自定义线程池和ComplatableFuture异步编排在springboot项目中的应用

(一)编写线程池的配置文件

@Configuration
public class MyThreadpool {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool){
        return new ThreadPoolExecutor(pool.getCoreSize(),pool.getMaxSize(),pool.getKeepAliveTime(), TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(1000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}

(二)自定义编写线程池参数配置,方便配置文件进行更改

@ConfigurationProperties(prefix = "zzc.thread")
@Component
public class ThreadPoolConfigProperties {
    private Integer coreSize;
    private Integer maxSize;
    private Integer keepAliveTime;
    private Integer dequeSize;

    public ThreadPoolConfigProperties() {
    }

    public ThreadPoolConfigProperties(Integer coreSize, Integer maxSize, Integer keepAliveTime,Integer dequeSize) {
        this.coreSize = coreSize;
        this.maxSize = maxSize;
        this.keepAliveTime = keepAliveTime;
        this.dequeSize=dequeSize;
    }

    public Integer getCoreSize() {
        return coreSize;
    }

    public void setCoreSize(Integer coreSize) {
        this.coreSize = coreSize;
    }

    public Integer getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(Integer maxSize) {
        this.maxSize = maxSize;
    }

    public Integer getKeepAliveTime() {
        return keepAliveTime;
    }

    public void setKeepAliveTime(Integer keepAliveTime) {
        this.keepAliveTime = keepAliveTime;
    }

    public Integer getDequeSize() {
        return dequeSize;
    }

    public void setDequeSize(Integer dequeSize) {
        this.dequeSize = dequeSize;
    }
}

(三)配置文件引入
可以在配置文件中自定义线程池参数

zzc.thread.coreSize=20
zzc.thread.maxSize=20
zzc.thread.keepAliveTime=20
zzc.thread.dequeSize=1000


(四)引入项目中

这里就用测试类中,换在业务中也是一样的逻辑

@SpringBootTest
class ThreadpoolApplicationTests {


    @Autowired
    ThreadPoolExecutor executor;


    @Test
    public void tesetCompletableFuture() throws ExecutionException, InterruptedException {
        /**
         * 销售属性组合,商品介绍,规格参数均依赖于基本信息
         */
        CompletableFuture<String> infoFuture = CompletableFuture.supplyAsync(() -> {
       
            String s = "基本信息获取";
            return s;
        }, executor);
        CompletableFuture<Void> saleFuture = infoFuture.thenAcceptAsync((s) -> {
            String ss = s + "::销售属性组合";
        },executor);
        CompletableFuture<Void> descFuture =infoFuture.thenAcceptAsync((s)->{
            String ss= "商品介绍";
        },executor);

        CompletableFuture<Void> parameFuture = infoFuture.thenAcceptAsync((s)->{
            String ss= "规格参数";
        },executor);
        //获取图片的异步任务,新开一个任务
        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            String ss = "获取图片";
        }, executor);

        //全部异步任务完成才返回
        CompletableFuture.allOf(infoFuture,saleFuture,descFuture,parameFuture,imageFuture).get();

    }

}

异步编排的应用场景可以参考电商详情页,采用异步的方式先查出基本信息,然后根据基本信息采用异步分方式分别获取销售属性组合、商品介绍、规格参数,获取图片的任务不依赖于基本信息,故可以新开一个异步任务去处理。最后等所有异步任务完成,调用allOf进行组合
参考在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值