Close

Java 8 Streams - Stream.parallel Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.BaseStream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamLogicBig

Method:

S parallel()

This intermediate operation returns an equivalent stream that is parallel. It may return itself, either because the stream was already parallel, or because the underlying stream state was modified to be parallel.


Examples


Sequential stream.

package com.logicbig.example.stream;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

public class ParallelExample2 {

public static void main(String... args) {

Stream.iterate(1L, a -> a + 1)
.limit(5)
.forEach(ParallelExample2::process);
}

private static void process(Long aLong) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(LocalDateTime.now());
}
}

Output

2017-05-01T15:44:01.806
2017-05-01T15:44:02.821
2017-05-01T15:44:03.828
2017-05-01T15:44:04.828
2017-05-01T15:44:05.842




package com.logicbig.example.stream;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

public class ParallelExample {

public static void main(String... args) {

Stream.iterate(1L, a -> a + 1)
.limit(5)
.parallel()
.forEach(ParallelExample::process);
}

private static void process(Long aLong) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(LocalDateTime.now());
}
}

Output

2017-05-01T15:43:58.907
2017-05-01T15:43:58.907
2017-05-01T15:43:58.907
2017-05-01T15:43:58.907
2017-05-01T15:43:58.907




This example shows the difference between Stream.parallel() and Stream.sequential(). Parallel streams uses multiple cores hence run the iterations in parallel, whereas, sequential streams just run all iterations in a single core one by one.

package com.logicbig.example;

import java.time.LocalTime;
import java.util.Arrays;
import java.util.stream.Stream;

public class SequentialParallelComparison {

public static void main (String[] args) {
String[] strings = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};

System.out.println("-------\nRunning sequential\n-------");
run(Arrays.stream(strings).sequential());
System.out.println("-------\nRunning parallel\n-------");
run(Arrays.stream(strings).parallel());
}

public static void run (Stream<String> stream) {

stream.forEach(s -> {
System.out.println(LocalTime.now() + " - value: " + s +
" - thread: " + Thread.currentThread().getName());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}

Output

-------
Running sequential
-------
15:43:53.521 - value: 1 - thread: com.logicbig.example.SequentialParallelComparison.main()
15:43:53.736 - value: 2 - thread: com.logicbig.example.SequentialParallelComparison.main()
15:43:53.945 - value: 3 - thread: com.logicbig.example.SequentialParallelComparison.main()
15:43:54.151 - value: 4 - thread: com.logicbig.example.SequentialParallelComparison.main()
15:43:54.362 - value: 5 - thread: com.logicbig.example.SequentialParallelComparison.main()
15:43:54.564 - value: 6 - thread: com.logicbig.example.SequentialParallelComparison.main()
15:43:54.773 - value: 7 - thread: com.logicbig.example.SequentialParallelComparison.main()
15:43:54.987 - value: 8 - thread: com.logicbig.example.SequentialParallelComparison.main()
15:43:55.196 - value: 9 - thread: com.logicbig.example.SequentialParallelComparison.main()
15:43:55.405 - value: 10 - thread: com.logicbig.example.SequentialParallelComparison.main()
-------
Running parallel
-------
15:43:55.618 - value: 7 - thread: com.logicbig.example.SequentialParallelComparison.main()
15:43:55.618 - value: 2 - thread: ForkJoinPool.commonPool-worker-3
15:43:55.618 - value: 9 - thread: ForkJoinPool.commonPool-worker-2
15:43:55.618 - value: 3 - thread: ForkJoinPool.commonPool-worker-1
15:43:55.618 - value: 8 - thread: ForkJoinPool.commonPool-worker-4
15:43:55.618 - value: 5 - thread: ForkJoinPool.commonPool-worker-5
15:43:55.618 - value: 10 - thread: ForkJoinPool.commonPool-worker-7
15:43:55.618 - value: 1 - thread: ForkJoinPool.commonPool-worker-6
15:43:55.831 - value: 6 - thread: ForkJoinPool.commonPool-worker-6
15:43:55.831 - value: 4 - thread: ForkJoinPool.commonPool-worker-5
Original Post




This example shows how laziness and parallelism mutually work.

package com.logicbig.example;

import java.util.stream.IntStream;

import static com.logicbig.example.LogUtil.log;

public class LazyParallelExample {

public static void main (String[] args) {
IntStream stream = IntStream.range(1, 5).parallel();
stream = stream.peek(i -> log("starting", i))
.filter(i -> {
log("filtering", i);
return i % 2 == 0;
})
.peek(i -> log("post filtering", i));
log("Invoking terminal method count.");
log("The count is", stream.count());
}
}

Output

15:43:51.708 - Invoking terminal method count.
15:43:51.719 - starting - 3
15:43:51.719 - starting - 2
15:43:51.719 - starting - 4
15:43:51.719 - starting - 1
15:43:51.722 - filtering - 2
15:43:51.722 - filtering - 1
15:43:51.722 - filtering - 3
15:43:51.722 - filtering - 4
15:43:51.723 - post filtering - 4
15:43:51.723 - post filtering - 2
15:43:51.725 - The count is - 2
Original Post




See Also