11package com .winterbe .java8 ;
22
3+ import java .util .ArrayList ;
34import java .util .Arrays ;
45import java .util .List ;
6+ import java .util .UUID ;
7+ import java .util .concurrent .ForkJoinPool ;
8+ import java .util .concurrent .TimeUnit ;
59
610/**
711 * @author Benjamin Winterberg
@@ -11,9 +15,36 @@ public class Streams12 {
1115 public static void main (String [] args ) {
1216 List <String > strings = Arrays .asList ("a1" , "a2" , "b1" , "c2" , "c1" );
1317
14- // test1(strings );
18+ // test1();
1519// test2(strings);
1620 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 ));
1748 }
1849
1950 private static void test3 (List <String > strings ) {
@@ -51,7 +82,7 @@ private static void test2(List<String> strings) {
5182 private static void test1 () {
5283 // -Djava.util.concurrent.ForkJoinPool.common.parallelism=5
5384
54- // ForkJoinPool commonPool = ForkJoinPool.commonPool();
55- // System.out.println(commonPool.getParallelism());
85+ ForkJoinPool commonPool = ForkJoinPool .commonPool ();
86+ System .out .println (commonPool .getParallelism ());
5687 }
5788}
0 commit comments