流式for循環
private static void test1() {
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
arrayList.add(i + "");
}
arrayList.stream().forEach(e -> System.out.println(Thread.currentThread().getName() + " " + e));
}
並行流式for循環
private static void test2() {
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
arrayList.add(i + "");
}
arrayList.parallelStream().forEach(e -> System.out.println(Thread.currentThread().getName() + " " + e));
}
forkjoin 並行流式for循環
private static final ForkJoinPool pool = new ForkJoinPool(5);
private static void test3() {
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
arrayList.add(i + "");
}
pool.submit(() -> arrayList.parallelStream().forEach(e -> {
System.out.println(Thread.currentThread().getName() + " " + e);
})).join();
}
執行
public static void main(String[] args) {
test1();
test2();
test3();
}