Skip to content

Commit 3806164

Browse files
committed
Add parallelStream examples
1 parent 4d556bd commit 3806164

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/com/winterbe/java8/Streams12.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.winterbe.java8;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* @author Benjamin Winterberg
8+
*/
9+
public class Streams12 {
10+
11+
public static void main(String[] args) {
12+
List<String> strings = Arrays.asList("a1", "a2", "b1", "c2", "c1");
13+
14+
// test1(strings);
15+
// test2(strings);
16+
test3(strings);
17+
}
18+
19+
private static void test3(List<String> strings) {
20+
strings
21+
.parallelStream()
22+
.filter(s -> {
23+
System.out.format("filter: %s [%s]\n", s, Thread.currentThread().getName());
24+
return true;
25+
})
26+
.map(s -> {
27+
System.out.format("map: %s [%s]\n", s, Thread.currentThread().getName());
28+
return s.toUpperCase();
29+
})
30+
.sorted((s1, s2) -> {
31+
System.out.format("sort: %s <> %s [%s]\n", s1, s2, Thread.currentThread().getName());
32+
return s1.compareTo(s2);
33+
})
34+
.forEach(s -> System.out.format("forEach: %s [%s]\n", s, Thread.currentThread().getName()));
35+
}
36+
37+
private static void test2(List<String> strings) {
38+
strings
39+
.parallelStream()
40+
.filter(s -> {
41+
System.out.format("filter: %s [%s]\n", s, Thread.currentThread().getName());
42+
return true;
43+
})
44+
.map(s -> {
45+
System.out.format("map: %s [%s]\n", s, Thread.currentThread().getName());
46+
return s.toUpperCase();
47+
})
48+
.forEach(s -> System.out.format("forEach: %s [%s]\n", s, Thread.currentThread().getName()));
49+
}
50+
51+
private static void test1() {
52+
// -Djava.util.concurrent.ForkJoinPool.common.parallelism=5
53+
54+
// ForkJoinPool commonPool = ForkJoinPool.commonPool();
55+
// System.out.println(commonPool.getParallelism());
56+
}
57+
}

0 commit comments

Comments
 (0)