FixedThreadPool线程池(记录一下)executor

    private String openThreadImportData( List<ZentaoBug> dataList) {

        int dataSize = dataList.size(); //初始化总数
        int threadSize = 1000;//线程容量,每5000条数据开启一条线程
        int threadNum = dataSize / threadSize + 1;// 线程数
        boolean special = dataSize % threadSize == 0;// 定义标记,过滤threadNum为整数

        // 创建一个固定可重用线程池
        ExecutorService exec = Executors.newFixedThreadPool(threadNum);

        // 线程个数 存货
        List<Future<Integer>> futures = new ArrayList<Future<Integer>>(threadNum);
        // 根据线程容量,为当前线程截取的数据
        List<ZentaoBug> subList = Lists.newArrayList();

        // 线程的个数
        for (int i = 0; i < threadNum; i++) {
            if (i == threadNum - 1) { // 最后一个线程
                if (special) {
                    break;
                }
                // 截取第n*ThreadSize个容量位置--至最后
                subList = dataList.subList(threadSize * i, dataSize);
            } else {
                // 截取的内容为  第n个容量位置--至 线程容量*(i+1),
                subList = dataList.subList(threadSize * i, threadSize * (i + 1));
            }
            final List<ZentaoBug> list = subList;
            Callable<Integer> task = () -> {
                List<ZentaoBug> batchList = Lists.newArrayList();

                // do 2 需要排除的数据 和已存在的数据
                List<ZentaoBug> isExistBugs = findisExistBug(list);
                Set<String> existBugIDs = isExistBugs.stream().map(ZentaoBug::getBugID).collect(Collectors.toSet());


                for (ZentaoBug zentaoBug : list) {

                    if (filterBug(zentaoBug)) {
                        continue;
                    }
                    //计算解决时间
                    computeZentaoBug(zentaoBug);
                              
                    //添加到集合
                    batchList.add(zentaoBug);
                }

                // do 3 保存活更新
                //批量保存
                this.saveBatch(batchList);

                return 1;
            };
            futures.add(exec.submit(task));// 添加线程
        }
        // 判断线程是否结束 exec.isShutdown
        exec.shutdown();// 关闭线程池
        //getResult(dataSize, filtedNum.get(), existNum.get())
       return  "上传成功";

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章