多线程使用,带返回值的线程池

业务需求拿到多个接口返回的信息集合,并通过判断对比整合数据,考虑可能并发比较高,所以引用线程池来完成;
可返回值的任务必须实现Callable接口。

执行Callable任务后,可以获取一个Future的对象,在该对象上调用get就可以获取到Callable任务返回的Object了。
创建一个线程池,在线程中调用其他接口来实现业务需求,并把结果返回,例子如下:

// 创建一个线程池
    ExecutorService executorService = Executors.newFixedThreadPool(3);
    List<Future> list = new ArrayList<>();
    // 执行任务并获取Future对象
    String requestId = LogUtil.getCurrentRequestId();
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    // 多线程实现商品状态线程
    Future goodsStautsFuture =
        executorService.submit(
            new Callable<String>() {
              @Override
              public String call() throws Exception {
                LogUtil.setRequestId(requestId);
                RequestContextHolder.setRequestAttributes(requestAttributes);
                GetCartOrderOhterMsgResultDto result =
                    orderPlaceOrderPageApiExecutorService.checkGoodsStauts(
                        listGoodsStatusParamsDto);
                result.setCode(1);
                logger.info("校验下单检查商品状态:result: {}", StringUtil.getJsonString(result));
                return StringUtil.getJsonString(result);
              }
            });
    list.add(goodsStautsFuture);
    // 获取商品价格信息
    Future checkSearchPriceFuture =
        executorService.submit(
            new Callable<String>() {
              @Override
              public String call() throws Exception {
                LogUtil.setRequestId(requestId);
                RequestContextHolder.setRequestAttributes(requestAttributes);
                GetCartOrderOhterMsgResultDto result =
                    orderPlaceOrderPageApiExecutorService.checkSearchPrice(
                        listPricePramasDto,
                        insertCartOrderParamDto,
                        Integer.valueOf(customerId.toString()));
                result.setCode(2);
                logger.info("校验获取商品价格:result: {}", StringUtil.getJsonString(result));
                return StringUtil.getJsonString(result);
              }
            });
    list.add(checkSearchPriceFuture);
    // 获取其他订单信息
    Future checkOtherMsgFuture =
        executorService.submit(
            new Callable<String>() {
              @Override
              public String call() throws Exception {
                LogUtil.setRequestId(requestId);
                RequestContextHolder.setRequestAttributes(requestAttributes);
                GetCartOrderOhterMsgResultDto result =
                    orderPlaceOrderPageApiExecutorService.checkOtherMsg(
                        insertCartOrderParamDto, Integer.valueOf(customerId.toString()));
                result.setCode(3);
                logger.info("校验获取其他订单信息:result: {}", StringUtil.getJsonString(result));
                return StringUtil.getJsonString(result);
              }
            });
    list.add(checkOtherMsgFuture);
    // 关闭线程池
    executorService.shutdown();

	for (Future f : list) {
        String msg=f.get().toString();
     }

在分布式程序中,调用其他功能需要用到requestid等信息,以前我们service层要想使用域对象必须先从Controller层中获取再传递过去,在线程池中会出现无法获取FeignClient的情况,这里做了特殊处理:
执行线程之前先获取
String requestId = LogUtil.getCurrentRequestId();
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
在线程中赋值
LogUtil.setRequestId(requestId);
RequestContextHolder.setRequestAttributes(requestAttributes);

线程创建的几种方式:

  1. 继承Thread类
  2. 实现Runnable接口
  3. 匿名类的方式
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值