1
1
package com .winterbe .java8 ;
2
2
3
+ import java .util .ArrayList ;
3
4
import java .util .Arrays ;
4
5
import java .util .List ;
6
+ import java .util .UUID ;
7
+ import java .util .concurrent .ForkJoinPool ;
8
+ import java .util .concurrent .TimeUnit ;
5
9
6
10
/**
7
11
* @author Benjamin Winterberg
@@ -11,9 +15,36 @@ public class Streams12 {
11
15
public static void main (String [] args ) {
12
16
List <String > strings = Arrays .asList ("a1" , "a2" , "b1" , "c2" , "c1" );
13
17
14
- // test1(strings );
18
+ // test1();
15
19
// test2(strings);
16
20
test3 (strings );
21
+ // test4();
22
+ }
23
+
24
+ private static void test4 () {
25
+ List <String > values = new ArrayList <>(100 );
26
+ for (int i = 0 ; i < 100 ; i ++) {
27
+ UUID uuid = UUID .randomUUID ();
28
+ values .add (uuid .toString ());
29
+ }
30
+
31
+ // sequential
32
+
33
+ long t0 = System .nanoTime ();
34
+
35
+ long count = values
36
+ .parallelStream ()
37
+ .sorted ((s1 , s2 ) -> {
38
+ System .out .format ("sort: %s <> %s [%s]\n " , s1 , s2 , Thread .currentThread ().getName ());
39
+ return s1 .compareTo (s2 );
40
+ })
41
+ .count ();
42
+ System .out .println (count );
43
+
44
+ long t1 = System .nanoTime ();
45
+
46
+ long millis = TimeUnit .NANOSECONDS .toMillis (t1 - t0 );
47
+ System .out .println (String .format ("parallel sort took: %d ms" , millis ));
17
48
}
18
49
19
50
private static void test3 (List <String > strings ) {
@@ -51,7 +82,7 @@ private static void test2(List<String> strings) {
51
82
private static void test1 () {
52
83
// -Djava.util.concurrent.ForkJoinPool.common.parallelism=5
53
84
54
- // ForkJoinPool commonPool = ForkJoinPool.commonPool();
55
- // System.out.println(commonPool.getParallelism());
85
+ ForkJoinPool commonPool = ForkJoinPool .commonPool ();
86
+ System .out .println (commonPool .getParallelism ());
56
87
}
57
88
}
0 commit comments