批量更新 分割list 多线程处理

南楼画角 提交于 2019-12-01 17:08:48
@Test
    public void testThreadPoolSplit() {
        List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 9, 8, 76, 41, 42, 43, 44);
        batchUpdate(ids);
    }

    public void batchUpdate(List<Integer> skuIds) {
        ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
        if (CollectionUtils.isNotEmpty(skuIds)) {
            int splitMaxNum = 7;
            //大量更新,超过20个,考虑多线程处理;
            if (skuIds.size() > splitMaxNum) {
                try {
                    for (int i = 0; i < skuIds.size(); i = i + splitMaxNum) {
                        List<Integer> newPurSkuList = skuIds.stream().skip(i).limit(splitMaxNum).collect(Collectors.toList());
                        executor.execute(() -> updateSku(newPurSkuList));
                    }
                    executor.shutdown();
                } catch (Exception e) {
                    log.warn("批量更新Sku 失败");
                }
            } else {
                updateSku(skuIds);
            }
        }
    }

    public void updateSku(List<Integer> skuList) {
        System.out.println(JSONObject.toJSONString(skuList));
    }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!