java利用线程池处理集合

java利用线程池处理集合

2018年07月23日 17:21:19 衍夏成歌 阅读数:866

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat\_38364990/article/details/81170003

java用线程池处理集合问题

循环集合每多少条数据开启一个集合,此处每十万数据开启一个线程

public void testStr() {
        List<BaseEntity> list = new ArrayList<>();
        for (int i = 0; i < 2000000; i++) {
            BaseEntity entity = new BaseEntity();
            entity.setId("这是**一个**测试" + i);
            list.add(entity);
        }

        long start = System.currentTimeMillis();
        check = new ChineseCheck();
        ExecutorService executor = Executors.newFixedThreadPool(5);
        int size = list.size();
        if (size > 100000) {
            int batch = size % 100000 == 0 ? size / 100000 : size / 100000 + 1;
            for (int j=0; j<batch; j++) {
                int end = (j+1)*100000;
                if (end > size) {
                    end = size;
                }
                List<BaseEntity> subList = list.subList(j*100000, end);
                TestCallable callable = new TestCallable(subList, check);
                executor.execute(callable);
            }
        }
        executor.shutdown();

        while (true) {
            if (executor.isTerminated()) {
                break;
            }
        }
        long date = System.currentTimeMillis() - start;
        System.out.println("======" + date + "======");
    }

用时:1361 ms 

两百万条数据做校验,每一条数据开一个线程

List<BaseEntity> list = new ArrayList<>();
        for (int i = 0; i < 2000000; i++) {
            BaseEntity entity = new BaseEntity();
            entity.setId("这是**一个**测试" + i);
            list.add(entity);
        }

        long start = System.currentTimeMillis();
        check = new ChineseCheck();
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (BaseEntity entity: list) {
            TestCallable callable = new TestCallable(entity, check);
            executor.execute(callable);
        }
        executor.shutdown();

        while (true) {
            if (executor.isTerminated()) {
                break;
            }
        }
        long date = System.currentTimeMillis() - start;
        System.out.println("======" + date + "======");

用时:29875 ms

以上两种情况对比,当使用线程池开启多线程的时候,每一个线程中校验多条数据,此时效率会高

可以按照这批数据的处理次数来创建线程池,规定线程池最大线程数,然后不大于这个线程数的时候可以按照处理次数来创建线程

此处,多个线程同用一个单例处理数据和多线程用不同的实例对象处理数据效果相同

即,此处循环list时,是否每次都new一个check

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid