多线程之CompletableFuture和ParallelStream应该使用哪个?

多线程之CompletableFuture和ParallelStream应该使用哪个

简介

  • 随着用户对性能和体验的要求越来越高,异步在项目中用的也就越来越多,CompletableFuture 和Parallel Stream无疑是异步并发的利器。既然两者都可以实现异步并发,那么就带来一个问题:什么时候该使用哪个呢?哪个场景下使用哪个会更好呢?这篇文章带你看看CompletableFuture与Parallel Stream的比较,从而可以由此知道什么场景下使用哪个。

实例场景

机器配置:macOS 六核处理器

测试代码

package io.ride.sensitive;

/**
 * @Description 使用这个类去构建一个运行长时间的任务-用于测试
 * @ClassName MyTask
 * @Author SunHui
 * @Date 2021/9/23 4:23 下午
 */
public class MyTask {
    private final int quantity;

    public MyTask (int quantity) {
        this.quantity = quantity;
    }

    public int calculate() {
        System.out.println(Thread.currentThread().getName());
        try {
            Thread.sleep(quantity * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return quantity;
    }
}

// 创建15个任务,每个任务执行时间是1秒
List<MyTask> tasks = IntStream.range(0, 15)
                .mapToObj(i -> new MyTask(1))
                .collect(toList());
package io.ride.sensitive;

import java.util.concurrent.*;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池管理的工具类,封装类
 *
 * @author SunHui
 */
public class ThreadPoolUtil {
    // 通过ThreadPoolExecutor的代理类来对线程池的管理
    private static ThreadPollProxy mThreadPollProxy;

    // 单列对象
    // 线程大小的规则:如果服务是cpu密集型的,设置为电脑的核数;如果服务是io密集型的,设置为电脑的核数*2
    public static ThreadPollProxy getThreadPollProxy() {
        synchronized (ThreadPollProxy.class) {
            if (mThreadPollProxy == null) {
                mThreadPollProxy = new ThreadPollProxy(Runtime.getRuntime().availableProcessors() * 2,
                        Runtime.getRuntime().availableProcessors() * 4,
                        30);
            }
        }
        return mThreadPollProxy;
    }

    // 通过ThreadPoolExecutor的代理类来对线程池的管理
    public static class ThreadPollProxy {
        public ThreadPoolExecutor poolExecutor;// 线程池执行者 ,java内部通过该api实现对线程池管理
        private int corePoolSize;
        private int maximumPoolSize;
        private long keepAliveTime;

        public ThreadPollProxy(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
            this.corePoolSize = corePoolSize;
            this.maximumPoolSize = maximumPoolSize;
            this.keepAliveTime = keepAliveTime;
            poolExecutor = new ThreadPoolExecutor(
                    // 核心线程数量
                    corePoolSize,
                    // 最大线程数量
                    maximumPoolSize,
                    // 当线程空闲时,保持活跃的时间
                    keepAliveTime,
                    // 时间单元 ,毫秒级
                    TimeUnit.SECONDS,
                    // 线程任务队列
                    new LinkedBlockingQueue<>(20000),
                    // 创建线程的工厂
                    Executors.defaultThreadFactory(),
                    // 拒绝策略,直接丢弃
                    new ThreadPoolExecutor.AbortPolicy());
        }

        //对外提供一个执行任务的方法
        public void execute(Runnable r) {
            if (poolExecutor == null || poolExecutor.isShutdown()) {
                poolExecutor = new ThreadPoolExecutor(
                        // 核心线程数量
                        corePoolSize,
                        // 最大线程数量
                        maximumPoolSize,
                        // 当线程空闲时,保持活跃的时间
                        keepAliveTime,
                        // 时间单元 ,毫秒级
                        TimeUnit.SECONDS,
                        // 线程任务队列
                        new LinkedBlockingQueue<>(20000),
                        // 创建线程的工厂
                        Executors.defaultThreadFactory(),
                        // 拒绝策略,直接丢弃
                        new ThreadPoolExecutor.AbortPolicy());
            }
            poolExecutor.execute(r);
        }
    }
}


开始测试

  • 正常串行执行
		long start = System.nanoTime();
        List<Integer> result = tasks.stream()
                .map(MyTask::calculate)
                .collect(toList());
        long duration = (System.nanoTime() - start) / 1_000_000;
        System.out.printf("Processed %d tasks in %d millis\n", tasks.size(), duration);
        System.out.println(result);

执行结果:

main
main
main
main
main
main
main
main
main
main
main
main
main
main
main
Processed 15 tasks in 15067 millis
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

结果就是我们所预期的,它花费了15秒,因为每个任务通过主线程一个接一个的执行。

  • 使用parallel stream
		long start = System.nanoTime();
        List<Integer> result = tasks.parallelStream()
                .map(MyTask::calculate)
                .collect(toList());
        long duration = (System.nanoTime() - start) / 1_000_000;
        System.out.printf("Processed %d tasks in %d millis\n", tasks.size(), duration);
        System.out.println(result);

执行结果:

main
ForkJoinPool.commonPool-worker-6
ForkJoinPool.commonPool-worker-4
ForkJoinPool.commonPool-worker-11
ForkJoinPool.commonPool-worker-2
ForkJoinPool.commonPool-worker-15
ForkJoinPool.commonPool-worker-8
ForkJoinPool.commonPool-worker-9
ForkJoinPool.commonPool-worker-1
ForkJoinPool.commonPool-worker-3
ForkJoinPool.commonPool-worker-13
ForkJoinPool.commonPool-worker-10
ForkJoinPool.commonPool-worker-3
ForkJoinPool.commonPool-worker-13
ForkJoinPool.commonPool-worker-15
Processed 15 tasks in 2060 millis
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

使用ParallelStream花费了2秒多,因为此次并发执行使用了12个线程 (11个是ForkJoinPool线程池中的,加上一个main线程),第一次执行使用了12个线程,但是任务并未执行完,因此执行完的线程去继续完成任务。

  • 使用CompletableFutures
long start = System.nanoTime();
        List<CompletableFuture<Integer>> futures =
                tasks.stream()
                        .map(t -> CompletableFuture.supplyAsync(t::calculate))
                        .collect(Collectors.toList());

        List<Integer> result =
                futures.stream()
                        .map(CompletableFuture::join)
                        .collect(Collectors.toList());
        long duration = (System.nanoTime() - start) / 1_000_000;
        System.out.printf("Processed %d tasks in %d millis\n", tasks.size(), duration);
        System.out.println(result);

执行结果:

ForkJoinPool.commonPool-worker-9
ForkJoinPool.commonPool-worker-4
ForkJoinPool.commonPool-worker-2
ForkJoinPool.commonPool-worker-11
ForkJoinPool.commonPool-worker-6
ForkJoinPool.commonPool-worker-13
ForkJoinPool.commonPool-worker-15
ForkJoinPool.commonPool-worker-8
ForkJoinPool.commonPool-worker-1
ForkJoinPool.commonPool-worker-10
ForkJoinPool.commonPool-worker-3
ForkJoinPool.commonPool-worker-10
ForkJoinPool.commonPool-worker-13
ForkJoinPool.commonPool-worker-6
ForkJoinPool.commonPool-worker-9
Processed 15 tasks in 2059 millis
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

和parallel stream一样使用的是默认的ForkJoinPool线程池,第一次使用了包括main线程在内的12个线程,由于线程数不够15个,因此先执行完的三个线程继续执行接下来的任务。

CompletableFutures大家应该不陌生,我们首先获取CompletableFutures集合,然后在每个future上调用join方法去等待他们逐一执行完。注意,join方法类似于get方法,唯一的不通点是前者不会抛出任何的受检查异常,所以在lambda表达式中更方便一些。

再有,你必须使用两个独立的stream(pipelines)管道,而不是将两个map操作放在一起,因为stream的中间操作都是懒加载的(intermediate stream operations are lazy),你最终必须按顺序处理你的任务。这就是为什么首先需要CompletableFuture在list中,然后允许他们开始执行,直到执行完毕。

  • 使用CompletableFutures并配合自定义线程池
ThreadPoolUtil.ThreadPollProxy threadPollProxy = ThreadPoolUtil.getThreadPollProxy();

long start = System.nanoTime();
        List<CompletableFuture<Integer>> futures =
                tasks.stream()
                        .map(t -> CompletableFuture.supplyAsync(t::calculate, threadPollProxy.poolExecutor))
                        .collect(Collectors.toList());

        List<Integer> result =
                futures.stream()
                        .map(CompletableFuture::join)
                        .collect(Collectors.toList());
        long duration = (System.nanoTime() - start) / 1_000_000;
        System.out.printf("Processed %d tasks in %d millis\n", tasks.size(), duration);
        System.out.println(result);

执行结果:

pool-1-thread-1
pool-1-thread-4
pool-1-thread-3
pool-1-thread-2
pool-1-thread-5
pool-1-thread-6
pool-1-thread-7
pool-1-thread-8
pool-1-thread-9
pool-1-thread-10
pool-1-thread-11
pool-1-thread-12
pool-1-thread-13
pool-1-thread-14
pool-1-thread-15
Processed 15 tasks in 1086 millis

本次使用的自定义线程池,核心线程数:2 * 6=12 最大线程数:4 * 6=24,线程数可以同时去执行当前所有任务,所以仅用了1秒钟。

CompletableFutures比parallel streams优点之一是你可以指定不用的Executor去处理他们的任务。这意味着基于你的项目,你能选择更合适数量的线程。我的例子不是cpu密集型的任务,我能选择增加大于Runtime.getRuntime().getAvailableProcessors()数量的线程,如上面所示:我的机器配置是六核,线程池配置了12个线程。

总结

  • CompletableFutures可以更多的控制线程池的数量。如果你的任务是io密集型的,你应该使用CompletableFutures。
  • 如果你的任务是cpu密集型的,使用比处理器更多的线程是没有意义的,所以选择parallel stream,因为它更容易使用。

备注

  • 如果测试的机器配置是1核甚至更低,这种情况如果使用自定义线程池就没有意义了,和单线程没什么区别,使用parallel stream默认的线程池就行了。(生产环境不会这么低)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术杠精

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值