Files
iot/iot-access/access-boot/src/test/java/com/njcn/BatchProcessing.java
2024-11-28 08:44:48 +08:00

43 lines
1.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.njcn;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class BatchProcessing {
public static void main(String[] args) {
// 创建包含10000条数据的列表
List<Integer> dataList = new ArrayList<>();
for (int i = 1; i <= 20000; i++) {
dataList.add(i);
}
// 将数据分成10个子列表每个子列表包含1000条数据
List<List<Integer>> batches = new ArrayList<>();
int batchSize = 1000;
int batchCount = dataList.size() / batchSize;
for (int i = 0; i < batchCount; i++) {
int fromIndex = i * batchSize;
int toIndex = fromIndex + batchSize;
List<Integer> batch = dataList.subList(fromIndex, toIndex);
batches.add(batch);
}
// 使用多线程并发处理每个子列表
ExecutorService executorService = Executors.newFixedThreadPool(10);
for (List<Integer> batch : batches) {
executorService.submit(() -> processBatch(batch));
}
executorService.shutdown();
}
private static void processBatch(List<Integer> batch) {
for (Integer data : batch) {
// 处理数据的逻辑
System.out.println(Thread.currentThread().getName() + ": processing data " + data);
}
}
}