多线程处理集合对象,批量插入

MyBatis 批量更新:

Mapper:

int batchUpdateTaskStatus(@Param("list") List list);

XML文件:

 
        
            update tablename set task_status = #{item.task_status} where id = #{item.id}
        
    

使用最简单的多线程批量更新

 /**
     * @description: 针对一个数据集合,采用多线程去批量更新
     * @return: void
     * @author: hezha
     * @time: 2024/1/25 11:09
     */
    public void batchUpdateTaskStatusConcurrently(List list) {
        try {
            long t1 = System.currentTimeMillis();
            int threadCount = 50;
            ExecutorService executor = Executors.newFixedThreadPool(threadCount);

            int size = list.size();

            //取整,每个线程处理的个数
            int subListSize = size / threadCount;
            //余数
            int remainder = size % threadCount;
            if (size <= threadCount) {
                threadCount = 1;
            }

            for (int i = 0; i < threadCount; i++) {
                int startIndex = i * subListSize;
                int endIndex = 0;
                if (threadCount == 1) {
                    endIndex = size;
                } else {
                    endIndex = (i + 1) * subListSize;
                }

//                System.out.println("i="+i+" --> startIndex="+startIndex + "  endIndex="+endIndex);
                List subList = list.subList(startIndex, endIndex);

                executor.execute(() -> taskMapper.batchUpdateTaskStatus(subList));
            }

            executor.shutdown();
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); // 等待所有任务完成
            if (remainder > 0) {
                List subList = list.subList(threadCount * subListSize, size);
//                System.out.println("last --> startIndex="+threadCount * subListSize + "  endIndex="+size);
                taskMapper.batchUpdateTaskStatus(subList);
            }
            log.info("thread 更新状态,花费时间=" + (System.currentTimeMillis() - t1) + ", 文件数据总量:" + size);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

总结: 使用多线程和非多线程批量插入,对于5000条数据,非多线程要17s左右,多线程之后就1s左右。

你可能感兴趣的:(java,mybatis)