美文网首页
线程池执和Future

线程池执和Future

作者: flyjar | 来源:发表于2020-12-11 09:05 被阅读0次
      //userIds是以逗号分割点的,拆分它,返回List
     List<String> userIdList=Arrays.asList(userIds.split(","));

    //获取offer的api只能一个用户ID获取一次,多个线程去获取,返回一个集合
    List<Future<List<Map<String,Object>>>> offerFutureList=userIdList.stream().map( (e)-> threadPoolExecutor.submit(() ->getHttpData(getofferapplyInfosById,e))).collect(Collectors.toList());

List<List<Map<String,Object>>> offerInfoList=new LinkedList<>();

   //Future get在lambda遍历时会有异常在循环中捕获,影响性能,所以采用foreach遍历。
  for (Future<List<Map<String,Object>>> future:offerFutureList) {

          offerInfoList.add(future.get());

    }

相关文章

网友评论

      本文标题:线程池执和Future

      本文链接:https://www.haomeiwen.com/subject/dpndgktx.html