43 lines
1.4 KiB
Java
43 lines
1.4 KiB
Java
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|