java parallelStream 线程堵塞问题笔记

发布时间 2023-07-19 13:55:12作者: 甜菜波波

定义:

Stream(流)是JDK8中引入的一种类似与迭代器(Iterator)的单向迭代访问数据的工具。ParallelStream则是并行的流,它通过Fork/Join 框架(JSR166y)来拆分任务,加速流的处理过程。最开始接触parallelStream很容易把其当做一个普通的线程池使用,因此也出现了上面提到的开始的时候打标,结束的时候去掉标的动作。

ForkJoinPool又是什么

ForkJoinPool是在Java 7中引入了一种新的线程池,其简单类图如下:

可以看到ForkJoinPool是ExecutorService的实现类,是一种线程池。创建了ForkJoinPool实例之后,可以通过调用submit(ForkJoinTask task) 或invoke(ForkJoinTask task)方法来执行指定任务。 ForkJoinTask表示线程池中执行的任务,其有两个主要的抽象子类:RecusiveAction和RecusiveTask。其中RecusiveTask代表有返回值的任务,而RecusiveAction代表没有返回值的任务。它们的类图如下:

 

ForkJoinPool来支持使用分治法(Divide-and-Conquer Algorithm)来解决问题,即将一个任务拆分成多个“小任务”并行计算,再把多个“小任务”的结果合并成总的计算结果。相比于ThreadPoolExecutor,ForkJoinPool能够在任务队列中不断的添加新任务,在线程执行完任务后可以再从任务列表中选择其他任务来执行;并且可以选择子任务的执行优先级,因此能够方便的执行具有父子关系的任务。ForkJoinPool内部维护了一个无限队列来保存需要执行的任务,而线程的数量则是通过构造函数传入,如果没有向构造函数中传入希望的线程数量,那么当前计算机可用的CPU数量会被设置为线程数量作为默认值(最大为MAX_CAP = 0x7fff)。

ParallelStream可能引起阻塞

对CPU密集型的任务来说,并行流使用ForkJoinPool,为每个CPU分配一个任务,这是非常有效率的,但是如果任务不是CPU密集的,而是I/O密集的,并且任务数相对线程数比较大,那么直接用ParallelStream并不是很好的选择。

 

  1.  
    package com.chenkang.test.util;
  2.  
     
  3.  
    import com.google.common.collect.Lists;
  4.  
     
  5.  
    import java.util.Date;
  6.  
    import java.util.List;
  7.  
    import java.util.concurrent.CountDownLatch;
  8.  
    import java.util.concurrent.TimeUnit;
  9.  
     
  10.  
    /**
  11.  
    * @author chenkang
  12.  
    * @date 2022/9/27 13:03
  13.  
    */
  14.  
    public class ParallelStream {
  15.  
     
  16.  
    /* public static void main(String[] args) throws InterruptedException {
  17.  
    List<Integer> lists = Lists.newArrayList();
  18.  
    TimeUnit.SECONDS.sleep(20);
  19.  
    //获取jvm 核数
  20.  
    System.out.println(Runtime.getRuntime().availableProcessors());
  21.  
    for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
  22.  
    lists.add(i);
  23.  
    }
  24.  
    Date start = new Date();
  25.  
    System.out.println(lists.size());
  26.  
    CountDownLatch countDownLatch= new CountDownLatch(3);
  27.  
    new Thread(()->{
  28.  
    ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
  29.  
    forkJoinPool.submit(() -> {
  30.  
    lists.parallelStream().forEach(e -> {
  31.  
    try {
  32.  
    TimeUnit.SECONDS.sleep(10);
  33.  
    } catch (InterruptedException e1) {
  34.  
    e1.printStackTrace();
  35.  
    }
  36.  
    });
  37.  
    System.out.println("执行1down");
  38.  
    countDownLatch.countDown();
  39.  
    });
  40.  
    }).start();
  41.  
     
  42.  
    new Thread(()->{
  43.  
    ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
  44.  
    forkJoinPool.submit(() -> {
  45.  
    lists.parallelStream().forEach(e -> {
  46.  
    try {
  47.  
    TimeUnit.SECONDS.sleep(10);
  48.  
    } catch (InterruptedException e1) {
  49.  
    e1.printStackTrace();
  50.  
    }
  51.  
    });
  52.  
    System.out.println("执行2down");
  53.  
    countDownLatch.countDown();
  54.  
    });
  55.  
    }).start();
  56.  
     
  57.  
    new Thread(()->{
  58.  
    ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
  59.  
    forkJoinPool.submit(() -> {
  60.  
    lists.parallelStream().forEach(e -> {
  61.  
    try {
  62.  
    TimeUnit.SECONDS.sleep(10);
  63.  
    } catch (InterruptedException e1) {
  64.  
    e1.printStackTrace();
  65.  
    }
  66.  
    });
  67.  
    System.out.println("执行3down");
  68.  
    countDownLatch.countDown();
  69.  
    });
  70.  
    }).start();
  71.  
     
  72.  
    countDownLatch.await();
  73.  
    Date end = new Date();
  74.  
     
  75.  
    System.out.println((end.getTime() - start.getTime()) / 1000);
  76.  
    }*/
  77.  
     
  78.  
    public static void main(String[] args) throws InterruptedException {
  79.  
    List<Integer> lists = Lists.newArrayList();
  80.  
    TimeUnit.SECONDS.sleep(20);
  81.  
    //获取jvm 核数
  82.  
    System.out.println(Runtime.getRuntime().availableProcessors());
  83.  
    for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
  84.  
    lists.add(i);
  85.  
    }
  86.  
    Date start = new Date();
  87.  
    System.out.println(lists.size());
  88.  
    CountDownLatch countDownLatch= new CountDownLatch(3);
  89.  
    new Thread(()->{
  90.  
    lists.parallelStream().forEach(e -> {
  91.  
    try {
  92.  
    TimeUnit.SECONDS.sleep(10);
  93.  
    } catch (InterruptedException e1) {
  94.  
    e1.printStackTrace();
  95.  
    }
  96.  
    });
  97.  
    System.out.println("执行1down");
  98.  
    countDownLatch.countDown();
  99.  
    }).start();
  100.  
     
  101.  
    new Thread(()->{
  102.  
    lists.parallelStream().forEach(e -> {
  103.  
    try {
  104.  
    TimeUnit.SECONDS.sleep(10);
  105.  
    } catch (InterruptedException e1) {
  106.  
    e1.printStackTrace();
  107.  
    }
  108.  
     
  109.  
    });
  110.  
    System.out.println("执行2down");
  111.  
    countDownLatch.countDown();
  112.  
    }).start();
  113.  
     
  114.  
    new Thread(()->{
  115.  
    lists.parallelStream().forEach(e -> {
  116.  
    try {
  117.  
    TimeUnit.SECONDS.sleep(10);
  118.  
    } catch (InterruptedException e1) {
  119.  
    e1.printStackTrace();
  120.  
    }
  121.  
    });
  122.  
    System.out.println("执行3down");
  123.  
    countDownLatch.countDown();
  124.  
    }).start();
  125.  
     
  126.  
    countDownLatch.await();
  127.  
    Date end = new Date();
  128.  
     
  129.  
    System.out.println((end.getTime() - start.getTime()) / 1000);
  130.  
    }
  131.  
     
  132.  
     
  133.  
     
  134.  
     
  135.  
    }

1.分别执行查看执行时间 链接jconsole 查看线程数量  这个是第二种 

 第一种

 可以看到线程数量提升 执行效率提高 第二种是因为commonpool 线程限制的原因

原文:https://www.csdn.net/tags/MtjaEg1sODI5MzctYmxvZwO0O0OO0O0O.html

https://blog.csdn.net/weixin_38845058/article/details/127070935