CompletableFuture代码示例

List<CompletableFuture<String>> checkFutures = checkFilterList.parallelStream().map(checkFilter-> 
                    CompletableFuture.supplyAsync(() -> {
                        String checkType = checkFilter.obtainCheckType();
                        if (!resultMap.containsKey(checkType)) {
                            StringBuilder strBuilder = new StringBuilder();
                            resultMap.put(checkType, strBuilder);
                        }
                        StringBuilder resultSaver = resultMap.get(checkType);
                        String result = StringConstant.JOB_SUCCESS;
                        try {
                            result = checkFilter.doFilter(sourceMediaDup, targetMediaDup, tableMappingInfo);
                        } catch (Exception e) {
                            logger.error("checkInputValid error happend", e);
                            result = StringConstant.JOB_ERROR;
                        }
                        if (!Objects.equals(result, StringConstant.JOB_SUCCESS)) {
                            resultSaver.append(result).append("\r\n");
                        }
                        return StringConstant.JOB_SUCCESS;
                    }, myExecutor).exceptionally(e -> {
                        logger.error("{} checkInputValid error", checkFilter.obtainCheckName(), e); //java.lang.ArithmeticException: / by zero
                        return "";
                    })).collect(Collectors.toList());
                checkFutures.stream().map(CompletableFuture::join).collect(Collectors.toList()); 

 

posted on 2021-01-28 20:08  MaXianZhe  阅读(94)  评论(0编辑  收藏  举报

导航