Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package java8.streams;
- import java.util.Arrays;
- import java.util.concurrent.ForkJoinPool;
- /*
- * This will demonstrate how parallelStream internally create pool for parallel stream execution. By defailt pool size is 3.
- * We can defined it by setting below JVM properties
- * -Djava.util.concurrent.ForkJoinPool.common.parallelism=5
- */
- public class ParallelStreamOperation {
- public static void main(String[] args) {
- //parallelStream internally create below pool
- ForkJoinPool commonPool = ForkJoinPool.commonPool();
- System.out.println(commonPool.getParallelism()); // 3
- System.out.println(commonPool.getQueuedTaskCount());
- System.out.println(commonPool.getCommonPoolParallelism());
- System.out.println(commonPool.getActiveThreadCount());
- //Example 1
- Arrays.asList("xy", "xz", "ab", "ac", "cb")
- .parallelStream()
- .filter(s -> {
- System.out.format("filter: %s [%s]\n",
- s, Thread.currentThread().getName());
- return true;
- })
- .map(s -> {
- System.out.format("map: %s [%s]\n",
- s, Thread.currentThread().getName());
- return s.toUpperCase();
- })
- .forEach(s -> System.out.format("forEach: %s [%s]\n",
- s, Thread.currentThread().getName()));
- //added shorting
- Arrays.asList("xy", "xz", "ab", "ac", "cb")
- .parallelStream()
- .filter(s -> {
- System.out.format("filter: %s [%s]\n",
- s, Thread.currentThread().getName());
- return true;
- })
- .map(s -> {
- System.out.format("map: %s [%s]\n",
- s, Thread.currentThread().getName());
- return s.toUpperCase();
- })
- .sorted((s1, s2) -> {
- System.out.format("sort: %s <> %s [%s]\n", s1, s2, Thread.currentThread().getName());
- return s1.compareTo(s2);
- })
- .forEach(s -> System.out.format("forEach: %s [%s]\n",
- s, Thread.currentThread().getName()));
- }
- }
Add Comment
Please, Sign In to add comment