2 回答
TA贡献1863条经验 获得超2个赞
Guava有Lists.partition和Iterables.partition的方法,它们可以做一些类似于你要求的事情。假设您有一个大列表,并希望以5个块的形式处理它,您可以执行以下操作:
int batchSize = 5; Lists.partition(list, batchSize) .parallelStream() .forEach(batch -> invokeList(batch));
TA贡献1784条经验 获得超2个赞
看起来非常冗长,但您可以尝试以下操作。该方法将使列表块并行运行。runAsync()
private void test(List<Object> list, int chunkSize) throws ExecutionException, InterruptedException {
AtomicInteger prev = new AtomicInteger(0);
List<CompletableFuture> futures = new ArrayList<>();
IntStream.range(1, (int) (chunkSize * (Math.ceil(Math.abs(list.size() / (double) chunkSize)))))
.filter(i -> i % chunkSize == 0 || i == list.size())
.forEach(i -> {
List<Object> chunk = list.subList(prev.get(), i);
futures.add(CompletableFuture.runAsync(() -> invokeList(chunk)));
prev.set(i);
});
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get();
}
private void invokeList(List<Object> list) {
System.out.println("Invoked for: " + list);
}
我为30个整数的列表运行它,块大小为5,如下所示:
public static void main(String[] args) throws ExecutionException, InterruptedException {
List<Object> list = IntStream.range(0, 30).mapToObj(i1 -> (Object) String.valueOf(i1)).collect(Collectors.toList());
int chunkSize = 5;
new Test().test(list, chunkSize);
}
输出:
Invoked for: [15, 16, 17, 18, 19]
Invoked for: [0, 1, 2, 3, 4]
Invoked for: [5, 6, 7, 8, 9]
Invoked for: [10, 11, 12, 13, 14]
Invoked for: [20, 21, 22, 23, 24]
添加回答
举报