码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 多线程处理大批量数据操作


     @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    private TransactionDefinition transactionDefinition;

    @Autowired
    private StudentService studentService;

    /**
     * 对用户而言,27s 任是一个较长的时间,我们尝试用多线程的方式来经行修改操作看能否加快处理速度
     * 预计创建10个线程,每个线程进行5000条数据修改操作
     * 耗时统计
     * 1 线程数:1      耗时:25s
     * 2 线程数:2      耗时:14s
     * 3 线程数:5      耗时:15s
     * 4 线程数:10     耗时:15s
     * 5 线程数:100    耗时:15s
     * 6 线程数:200    耗时:15s
     * 7 线程数:500    耗时:17s
     * 8 线程数:1000    耗时:19s
     * 8 线程数:2000    耗时:23s
     * 8 线程数:5000    耗时:29s
     */
    @Test
    void updateStudentWithThreads() {
        //查询总数据
        List allStudents = studentMapper.getAll();
        // 线程数量
        final Integer threadCount = 100;

        //每个线程处理的数据量
        final Integer dataPartionLength = (allStudents.size() + threadCount - 1) / threadCount;

        // 创建多线程处理任务
        ExecutorService studentThreadPool = Executors.newFixedThreadPool(threadCount);
        CountDownLatch threadLatchs = new CountDownLatch(threadCount);

        for (int i = 0; i < threadCount; i++) {
            // 每个线程处理的数据
            List threadDatas = allStudents.stream()
                    .skip(i * dataPartionLength).limit(dataPartionLength).collect(Collectors.toList());
            studentThreadPool.execute(() -> {
                studentService.updateStudents(threadDatas, threadLatchs);
            });
        }
        try {
            // 倒计时锁设置超时时间 30s
            threadLatchs.await(30, TimeUnit.SECONDS);
        } catch (Throwable e) {
            e.printStackTrace();
        }

        System.out.println("主线程完成");
    }

  • 相关阅读:
    Unity-协同程序原理
    Android 10.0 SystemUI状态栏显示和隐藏自定义图标
    stable-diffusion-webui-forge 介绍,安装,运行
    基于AI+视频智能分析技术的SkeyeVSS建筑废弃物监管解决方案
    RabbitMQ(五) | MQ集群搭建、部署、仲裁队列、集群扩容
    我与随机红包算法的故事
    高并发研究
    【LeetCode每日一题】——225.用队列实现栈
    ssm欢欢宠物医院管理系统的设计与实现毕业设计源码171734
    计算机视觉驾驶行为识别应用简述
  • 原文地址:https://blog.csdn.net/weixin_45958605/article/details/132739156
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号