多线程之parallelStream

        parallelStream默认使用线程池ForkJoinPool来调度的,而ForkJoinPool的默认线程数是CPU核数 - 1

举例一 : 不使用线程池

public static void main(String[] args) throws ExecutionException, InterruptedException {
    ArrayList<Integer> list = new ArrayList<>(1000);
    ExecutorService executorService = Executors.newFixedThreadPool(6);
    for (int i = 0; i < 1000; i++) {
        list.add(i);
    }

    Map<String, String> concurrentMap = new ConcurrentHashMap<>();
    list.parallelStream().forEach(res -> {
        concurrentMap.put(Thread.currentThread().getName(), Thread.currentThread().getName());
        System.out.println();
    });
    System.out.println("main thread over ....");
    concurrentMap.keySet().forEach(System.out::println);
}

输出:使用spring框架中的 ForkJoinPool.commonPool

main thread over ....
ForkJoinPool.commonPool-worker-13
ForkJoinPool.commonPool-worker-12
ForkJoinPool.commonPool-worker-15
ForkJoinPool.commonPool-worker-14
main
ForkJoinPool.commonPool-worker-9
ForkJoinPool.commonPool-worker-7
ForkJoinPool.commonPool-worker-8
ForkJoinPool.commonPool-worker-1
ForkJoinPool.commonPool-worker-2
ForkJoinPool.commonPool-worker-5
ForkJoinPool.commonPool-worker-6
ForkJoinPool.commonPool-worker-3
ForkJoinPool.commonPool-worker-11
ForkJoinPool.commonPool-worker-4
ForkJoinPool.commonPool-worker-10

举例二:使用其他线程池,如 Executors.newFixedThreadPool(6) 定义6个固定线程的线程池

public static void main(String[] args) throws ExecutionException, InterruptedException {
    ArrayList<Integer> list = new ArrayList<>(1000);
    ExecutorService executorService = Executors.newFixedThreadPool(6);
    for (int i = 0; i < 1000; i++) {
        list.add(i);
    }
    Map<String, String> concurrentMap = new ConcurrentHashMap<>();
    executorService.submit(() -> {
        list.parallelStream().forEach(res -> {
            concurrentMap.put(Thread.currentThread().getName(), Thread.currentThread().getName());
            System.out.println();
        });
    }).get();
    
    System.out.println("main thread over ....");
    concurrentMap.keySet().forEach(System.out::println);
}

输出:使用的依旧是 ForkJoinPool.commonPool

main thread over ....
ForkJoinPool.commonPool-worker-13
ForkJoinPool.commonPool-worker-12
ForkJoinPool.commonPool-worker-15
ForkJoinPool.commonPool-worker-14
pool-2-thread-1
ForkJoinPool.commonPool-worker-9
ForkJoinPool.commonPool-worker-7
ForkJoinPool.commonPool-worker-8
ForkJoinPool.commonPool-worker-1
ForkJoinPool.commonPool-worker-2
ForkJoinPool.commonPool-worker-5
ForkJoinPool.commonPool-worker-6
ForkJoinPool.commonPool-worker-3
ForkJoinPool.commonPool-worker-11
ForkJoinPool.commonPool-worker-10
ForkJoinPool.commonPool-worker-4

举例三:使用自定义ForkJoinPool

public static void main(String[] args) throws ExecutionException, InterruptedException {
    ArrayList<Integer> list = new ArrayList<>(1000);
    ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
    for (int i = 0; i < 1000; i++) {
        list.add(i);
    }
    Map<String, String> concurrentMap = new ConcurrentHashMap<>();
    forkJoinPool.submit(() -> {
        list.parallelStream().forEach(res -> {
            concurrentMap.put(Thread.currentThread().getName(), Thread.currentThread().getName());
            System.out.println();
        });
    }).get();
    
    System.out.println("main thread over ....");
    concurrentMap.keySet().forEach(System.out::println);
}

输出:使用自定的ForkJoinPool线程池

main thread over ....
ForkJoinPool-1-worker-8
ForkJoinPool-1-worker-9
ForkJoinPool-1-worker-6
ForkJoinPool-1-worker-7
ForkJoinPool-1-worker-12
ForkJoinPool-1-worker-11
ForkJoinPool-1-worker-10
ForkJoinPool-1-worker-1
ForkJoinPool-1-worker-4
ForkJoinPool-1-worker-5
ForkJoinPool-1-worker-2
ForkJoinPool-1-worker-3
ForkJoinPool-1-worker-16
ForkJoinPool-1-worker-15
ForkJoinPool-1-worker-14
ForkJoinPool-1-worker-13

总结:在使用parallelStream时要注意根据需求选择合适的线程池,避免出现线程池溢出等一系列问题。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值