线程池多线程并发处理批量数据

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolTool<T> {

    //单个线程处理的数据量
    private int singleCount;
    //处理的总数据量
    private int listSize;
    //开启的线程数
    private int runSize;
    //操作的数据集
    private List<T> list;
    //计数器
    private CountDownLatch begin, end;
    //线程池
    private ExecutorService executorService;
    //回调
    private CallBack callBack;

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public ThreadPoolTool(int singleCount, List<T> list) {
        this.singleCount = singleCount;
        this.list = list;
        if (list != null) {
            this.listSize = list.size();
            this.runSize = (this.listSize / this.singleCount) + 1;
        }
    }

    public void excute() throws InterruptedException {
        executorService = Executors.newFixedThreadPool(runSize);
        begin = new CountDownLatch(1);
        end = new CountDownLatch(runSize);
        //创建线程
        int startIndex = 0;
        int endIndex = 0;
        List<T> newList = null;
        for (int i = 0; i < runSize; i++) {
            //计算每个线程对应的数据
            if (i < (runSize - 1)) {
                startIndex = i * singleCount;
                endIndex = (i + 1) * singleCount;
                newList = list.subList(startIndex, endIndex);
            } else {
                startIndex = i * singleCount;
                endIndex = listSize;
                newList = list.subList(startIndex, endIndex);
            }
            //创建线程类处理数据
            MyThread<T> myThread = new MyThread(newList, begin, end) {
                @Override
                public void method(List list) {
                    callBack.method(list);
                }
            };
            //执行线程
            executorService.execute(myThread);
        }
        //计数器减一
        begin.countDown();
        end.await();
        //关闭线程池
        executorService.shutdown();
    }

    //抽象线程类
    public abstract class MyThread<T> implements Runnable {

        private List<T> list;
        private CountDownLatch begin, end;

        public MyThread(List<T> list, CountDownLatch begin, CountDownLatch end) {
            this.list = list;
            this.begin = begin;
            this.end = end;
        }

        @Override
        public void run() {
            try {
                //执行程序
                method(list);
                begin.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                //计数器减一
                end.countDown();
            }
        }

        public abstract void method(List<T> list);
    }

    //回调接口定义
    public interface CallBack<T> {
        public void method(List<T> list);
    }


    public static void main(String[] agrs) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            list.add("hello" + i);
        }
        Long inserOrUpdateBegin = System.currentTimeMillis();
        System.out.println(("数据更新开始时间:"+inserOrUpdateBegin));
        ThreadPoolTool<String> tool = new ThreadPoolTool(100, list);
        tool.setCallBack(new CallBack<String>() {
            @Override
            public void method(List<String> list) {
                for (int i = 0; i < list.size(); i++) {
                    System.out.print(Thread.currentThread().getId() + ":" + list.get(i) + " ");
                }
                System.out.println();
            }
        });
        try {
            tool.excute();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            Long inserOrUpdateEnd = System.currentTimeMillis();
            System.out.println(("数据更新结束时间:"+inserOrUpdateEnd+"。此次更新数据花费时间为:"+(inserOrUpdateEnd-inserOrUpdateBegin)));
        }
    }

}

好的,针对您的问题,我可以给您提供以下的解决方案: 1. 创建线程池:使用Java中的Executor框架创建线程池。 2. 准备数据:将要插入的数据准备好,可以使用List或其他数据结构来保存。 3. 分批处理数据:将数据分批处理,每批处理一定数量的数据。这样可以避免一次性插入大批量数据导致的内存溢出等问题。 4. 使用多线程处理数据:将每一批数据提交到线程池中,让线程池中的线程并发处理数据插入操作。 5. 关闭线程池:在所有数据插入完成后,关闭线程池释放资源。 以下是一个示例代码,供您参考: ``` ExecutorService executor = Executors.newFixedThreadPool(10); // 创建线程池,设置最大线程数为10 List<Data> dataList = prepareData(); // 准备数据 int batchSize = 1000; // 每批处理1000条数据 int totalSize = dataList.size(); int batchCount = (totalSize + batchSize - 1) / batchSize; // 计算需要分批处理的次数 for (int i = 0; i < batchCount; i++) { int startIndex = i * batchSize; int endIndex = Math.min((i + 1) * batchSize, totalSize); List<Data> subList = dataList.subList(startIndex, endIndex); // 获取当前批次的数据 executor.submit(() -> { insertBatchData(subList); // 将当前批次的数据插入到数据库中 }); } executor.shutdown(); // 关闭线程池 ``` 需要注意的是,insertBatchData方法需要保证线程安全,避免多线程操作同一个连接池或数据库连接导致的并发问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值