java8 异步编排优化

CompletableFuture是JDK8提出的一个支持非阻塞的多功能的Future,同样也是实现了Future接口

这里只介绍咱们开发中经常用的

@Configuration
public class MyThreadConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(){

        return new ThreadPoolExecutor(20, 200, 10 , TimeUnit.SECONDS, new LinkedBlockingDeque<>(10000), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
    }
}
	@Autowired
    private ThreadPoolExecutor executor;
    
	@RequestMapping(value = "/loanThread", method = RequestMethod.POST)
    public RestResponse loanThread(Long orderId){
        AceEntity aceEntity = new AceEntity();

		//1.将查询的结果返回
        CompletableFuture<OrderInfo> orderInfoFuture = CompletableFuture.supplyAsync(() -> {
            OrderInfo orderInfo = orderInfoBiz.findById(orderId);;
            aceEntity.setOrderID(orderInfo.getId().toString());
            return orderInfo;
        }, executor);

		//取1中的的客户id
        CompletableFuture<Void> custInfoFuture = orderInfoFuture.thenAcceptAsync(res -> {
            CustInfo custInfo = custInfoBiz.findById(res.getCustInfoId());
            aceEntity.setClientID(custInfo.getId().intValue());
        }, executor);
        
        try {
          //等所有任务执行完再执行主线程
           CompletableFuture.allOf(orderInfoFuture,custInfoFuture).get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        RestResponse restResponse = aceService.loan(aceEntity);
        log.info("loan cost:=======>{}==========mills",CountRunTime.end());
        return restResponse;
    }

这只是例子 这个是去调用第三方接口需要去查多张表 我将各个查询用异步任务去执行

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值