Compare the efficiency of serial and parallel flow streams:
/** * @author WGR * @create 2020/3/31 */ public class Demo07Parallel { private static final int times = 500000000; long start; @Before public void init() { start = System.currentTimeMillis(); } @After public void destory() { long end = System.currentTimeMillis(); System.out.println("消耗时间:" + (end - start)); } //Parallel Stream: Time consumption: 431 @Test public void testParallelStream () { LongStream.rangeClosed ( . 0, Times) .parallel () the reduce (0 , Long :: SUM); } // serial Stream: Time consumption: 623 @Test public void testStream () { // get 500,000,000 numbers, and summed LongStream.rangeClosed (0, Times) .reduce (0 , Long :: SUM); } }
We can see parallelStream efficiency is the highest.
Stream parallel processing process will divide and conquer, which is a big task into multiple smaller tasks, each of which represents a task is operating.
The following specific experiments to do it:
@Test public void test0Serial() { long count = Stream.of(4, 5, 3, 9, 1, 2, 6) .filter(s -> { System.out.println(Thread.currentThread() + ", s = " + s); return true; }) .count(); System.out.println("count = " + count); }
Parallel streams:
@Test public void test0Parallel () { Long COUNT = Stream.of (. 4,. 5,. 3,. 9,. 1, 2,. 6 ) .parallel () // to flow into concurrent streams, Stream processing time before going to the .filter (S -> { System.out.println (Thread.currentThread () + ", S =" + S); return to true ; }) .count (); System.out.println ( "COUNT =" + COUNT); }
Obtain parallel streams in two ways:
direct access parallel stream: parallelStream ()
serial flow into parallel streams: Parallel ()
Parallel thread-safety issues streams:
@Test public void parallelStreamNotice() { ArrayList<Integer> list = new ArrayList<>(); IntStream.rangeClosed(1, 1000) .parallel() .forEach(i -> { list.add(i); }); System.out.println("list = " + list.size()); }
@Test public void parallelStreamNotice () { the ArrayList <Integer> = List new new the ArrayList <> (); // IntStream.rangeClosed (. 1, 1000) // .parallel () // .forEach (I -> { // List. the Add (I); // }); // System.out.println ( "List =" + list.size ()); // solve parallelStream security thread program: synchronous block Object obj = new new Object ( ); IntStream.rangeClosed ( . 1, 1000 ) .parallel () .forEach (I -> { synchronized (obj) { list.add(i); } }); System.out.println("list = " + list.size()); }
// parallelStream security thread @Test public void parallelStreamNotice () { the ArrayList <Integer> = List new new the ArrayList <> (); // IntStream.rangeClosed (. 1, 1000) // .parallel () // .forEach (I - > { // List.add (I); // }); // System.out.println ( "List =" + list.size ()); // solve parallelStream security thread program: synchronous block // Object obj = new new Object (); // IntStream.rangeClosed (. 1, 1000) // .parallel () // .forEach (I -> { // the synchronized (obj) { // List.add (I); // } // }); // solve parallelStream security thread Option II: thread-safe set the Vector <Integer> = V new new the Vector (); List <Integer> = synchronizedList the Collections.synchronizedList (List); IntStream.rangeClosed ( . 1, 1000 ) .parallel () .forEach (I -> { synchronizedList.add (I ); }); the System. out.println ("list = " + synchronizedList.size()); }
// parallelStream线程安全问题 @Test public void parallelStreamNotice() { List<Integer> collect = IntStream.rangeClosed(1, 1000) .parallel() .boxed() .collect(Collectors.toList()); System.out.println("collect.size = " + collect.size()); }