码迷,mamicode.com
首页 > 编程语言 > 详细

java线程

时间:2017-12-23 23:09:34      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:ogr   final   message   orb   and   puts   override   ons   rtt   

@Override
    public void excelImportProgramAndDetail(String awardItemCode, InputStream excelInputStream, String fileName) {
        Preconditions.checkArgument(excelInputStream != null, "excel数据不能为空");

        File configFile = new File(SalaryExcelConfig.CLASSPATH_EXCEL_PROPERTIES +
                SalaryExcelConfig.getPropertiesHolder().getString("cn.com.tisco.hr.salary.excel.json.cfg.awardDetail"));
        ExcelImporter excelImporter = ExcelImporter.excel(excelInputStream, fileName).forSheet(1);

        List<AwardProgramDetail> awardProgramDetailList = excelImporter.configSheet(configFile)
                .parseSheet()
                .validate()
                .getBeans();

        // 当导入的单项奖明细数据在1000条以上时,采用多线程的方式分批导入数据。
        if(awardProgramDetailList.size() <= 20000) {
            doImportProgramAndDetail(awardItemCode, excelImporter, awardProgramDetailList);
        } else {
            int threadCount = awardProgramDetailList.size() / 1000 + 1;

            ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
            CompletionService<Boolean> completionService = new ExecutorCompletionService<>(executorService);
            List<Future<Boolean>> executionResults = new ArrayList<>(threadCount);

            for(int i = 0; i < awardProgramDetailList.size(); i++) {
                Future<Boolean> future = completionService.submit(makeProgramAndDetailImportThread(
                        awardProgramDetailList.subList(i * 1000, i * 1000 + 1000),
                        awardItemCode, excelImporter));

                executionResults.add(future);
            }

            boolean successful = true;
            for(int i=0; i < executionResults.size(); i++) {
                try {
                    Boolean result = completionService.take().get();
                    if(Boolean.FALSE.equals(result)) {
                        successful = false;
                    }
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            }

            if(! successful) {
                throw new IllegalStateException("单项奖数据导入时发生错误。");
            }
        }
    }
 private Callable<Boolean> makeProgramAndDetailImportThread(
                           final List<AwardProgramDetail> awardProgramDetails,
                           final String awardItemCode, final ExcelImporter excelImporter) {
        return new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                try {
                    doImportProgramAndDetail(awardItemCode, excelImporter, awardProgramDetails);
                    return Boolean.TRUE;
                } catch (Exception e) {
                    logger.error("导入单项奖[代码=" + awardItemCode + "]时发生异常。", e);
                    excelImporter.getErrorBuilder().addSheetGlobalError(e.getMessage());
                    return Boolean.FALSE;
                }
            }
        };
    }

 

java线程

标签:ogr   final   message   orb   and   puts   override   ons   rtt   

原文地址:http://www.cnblogs.com/wxx516/p/8094483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!