赞
踩
下面是一个用 Java 实现的多线程异步批量入库的示例:
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
-
- public class BatchInsertDemo {
-
- private static final int BATCH_SIZE = 1000; // 每批次入库的数据量
- private static final int THREAD_POOL_SIZE = 5; // 线程池大小
- private static final int TOTAL_COUNT = 10000; // 总共需要入库的数据量
-
- public static void main(String[] args) {
-
- // 模拟数据
- List<Data> dataList = new ArrayList<>();
- for (int i = 1; i <= TOTAL_COUNT; i++) {
- dataList.add(new Data(i));
- }
-
- // 创建线程池
- ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
-
- // 按批次提交任务
- int start = 0;
- while (start < TOTAL_COUNT) {
- int end = Math.min(start + BATCH_SIZE, TOTAL_COUNT);
- List<Data> batchList = dataList.subList(start, end);
- BatchInsertTask task = new BatchInsertTask(batchList);
- executorService.submit(task);
- start = end;
- }
-
- // 关闭线程池
- executorService.shutdown();
- }
-
- // 数据对象
- private static class Data {
- private int id;
- public Data(int id) {
- this.id = id;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- }
-
- // 入库任务
- private static class BatchInsertTask implements Runnable {
-
- private List<Data> dataList;
-
- public BatchInsertTask(List<Data> dataList) {
- this.dataList = dataList;
- }
-
- @Override
- public void run() {
- // 执行批量入库逻辑
- System.out.printf("开始入库,当前批次共有 %d 条数据\n", dataList.size());
- try {
- Thread.sleep(1000); // 模拟入库耗时
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.printf("完成入库,当前批次共有 %d 条数据\n", dataList.size

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。