美文网首页
多线程往数据库批量insert

多线程往数据库批量insert

作者: 刘小刀tina | 来源:发表于2021-12-11 20:20 被阅读0次
 @PostMapping
    public Object batchInsetAsync(){
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        int count = LIST.size();
        int threadSize = count % 3000 == 0 ? count / 3000 : (count / 3000) + 1;
        List<Callable<Boolean>> tasks = new ArrayList<>();
        for (int i = 0; i < threadSize; i++) {
            //0,3000,3000 6000, 6000 6001
            int k = i;
            int start = k * 3000;
            int index = (k + 1) * 3000;
            tasks.add(() -> {
                int end = k == threadSize-1 ? count : index;
                //业务批量插入
                List<Model> models = LIST.subList(start, end);
                modelMapper.batchInsert(models);
                log.info("currentTime:{},threadName:{}", Instant.now(),Thread.currentThread().getName());
                return Boolean.TRUE;
            });
        }
        try {
            executorService.invokeAll(tasks);
        } catch (InterruptedException e) {
            log.error("e:{}",e);
            e.printStackTrace();
        }
        executorService.shutdown();
        return "success2222";
    }


相关文章

网友评论

      本文标题:多线程往数据库批量insert

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