Guest User

Untitled

a guest
Dec 17th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. package java8.streams;
  2.  
  3. import java.util.Arrays;
  4. import java.util.concurrent.ForkJoinPool;
  5. /*
  6. * This will demonstrate how parallelStream internally create pool for parallel stream execution. By defailt pool size is 3.
  7. * We can defined it by setting below JVM properties
  8. * -Djava.util.concurrent.ForkJoinPool.common.parallelism=5
  9. */
  10. public class ParallelStreamOperation {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. //parallelStream internally create below pool
  15. ForkJoinPool commonPool = ForkJoinPool.commonPool();
  16. System.out.println(commonPool.getParallelism()); // 3
  17. System.out.println(commonPool.getQueuedTaskCount());
  18. System.out.println(commonPool.getCommonPoolParallelism());
  19. System.out.println(commonPool.getActiveThreadCount());
  20.  
  21. //Example 1
  22. Arrays.asList("xy", "xz", "ab", "ac", "cb")
  23. .parallelStream()
  24. .filter(s -> {
  25. System.out.format("filter: %s [%s]\n",
  26. s, Thread.currentThread().getName());
  27. return true;
  28. })
  29. .map(s -> {
  30. System.out.format("map: %s [%s]\n",
  31. s, Thread.currentThread().getName());
  32. return s.toUpperCase();
  33. })
  34. .forEach(s -> System.out.format("forEach: %s [%s]\n",
  35. s, Thread.currentThread().getName()));
  36.  
  37. //added shorting
  38. Arrays.asList("xy", "xz", "ab", "ac", "cb")
  39. .parallelStream()
  40. .filter(s -> {
  41. System.out.format("filter: %s [%s]\n",
  42. s, Thread.currentThread().getName());
  43. return true;
  44. })
  45. .map(s -> {
  46. System.out.format("map: %s [%s]\n",
  47. s, Thread.currentThread().getName());
  48. return s.toUpperCase();
  49. })
  50. .sorted((s1, s2) -> {
  51. System.out.format("sort: %s <> %s [%s]\n", s1, s2, Thread.currentThread().getName());
  52. return s1.compareTo(s2);
  53. })
  54. .forEach(s -> System.out.format("forEach: %s [%s]\n",
  55. s, Thread.currentThread().getName()));
  56. }
  57.  
  58. }
Add Comment
Please, Sign In to add comment