自有网站建设的团队,国外网站建设费用,南京网站维护,站长论坛java8并行流这篇文章展示了Java 8的CompletableFuture在执行异步计算时如何与并行流进行比较。 我们将使用以下类对长时间运行的任务进行建模#xff1a; class MyTask {private final int duration;public MyTask(int duration) {this.duration duration;}public int calc… java8并行流 这篇文章展示了Java 8的CompletableFuture在执行异步计算时如何与并行流进行比较。 我们将使用以下类对长时间运行的任务进行建模 class MyTask {private final int duration;public MyTask(int duration) {this.duration duration;}public int calculate() {System.out.println(Thread.currentThread().getName());try {Thread.sleep(duration * 1000);} catch (final InterruptedException e) {throw new RuntimeException(e);}return duration;}
} 让我们创建十个任务每个任务持续1秒 ListMyTask tasks IntStream.range(0, 10).mapToObj(i - new MyTask(1)).collect(toList()); 我们如何有效地计算任务清单 方法1依次 您首先想到的是按顺序计算任务如下所示 public static void runSequentially(ListMyTask tasks) {long start System.nanoTime();ListInteger 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);
} 如您所料这需要10秒钟才能运行因为每个任务都在main线程上一个接一个地运行。 方法2使用并行流 快速改进是将您的代码转换为使用并行流如下所示 public static void useParallelStream(ListMyTask tasks) {long start System.nanoTime();ListInteger 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-1
ForkJoinPool.commonPool-worker-3
ForkJoinPool.commonPool-worker-2
ForkJoinPool.commonPool-worker-3
ForkJoinPool.commonPool-worker-2
main
ForkJoinPool.commonPool-worker-1
ForkJoinPool.commonPool-worker-1
main
Processed 10 tasks in 3043 millis 这次花了3秒因为并行执行了4个任务使用了来自ForkJoinPool三个线程以及main线程。 方法3使用CompletableFutures 让我们看看CompletableFuture的性能是否更好 public static void useCompletableFuture(ListMyTask tasks) {long start System.nanoTime();ListCompletableFutureInteger futures tasks.stream().map(t - CompletableFuture.supplyAsync(() - t.calculate())).collect(Collectors.toList());ListInteger 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);
} 在上面的代码中我们首先获取CompletableFuture的列表然后在每个CompletableFuture调用join方法以等待它们CompletableFuture完成。 请注意 join与get相同唯一的区别是前者不引发任何检查的异常因此在lambda表达式中更为方便。 另外您必须使用两个单独的流管道而不是将两个map操作彼此放在后面因为中间流操作是惰性的您将不得不按顺序处理任务 这就是为什么您首先需要在列表中收集CompletableFuture 以允许它们在等待完成之前启动。 输出是 ForkJoinPool.commonPool-worker-1
ForkJoinPool.commonPool-worker-2
ForkJoinPool.commonPool-worker-3
ForkJoinPool.commonPool-worker-1
ForkJoinPool.commonPool-worker-2
ForkJoinPool.commonPool-worker-3
ForkJoinPool.commonPool-worker-1
ForkJoinPool.commonPool-worker-2
ForkJoinPool.commonPool-worker-3
ForkJoinPool.commonPool-worker-1
Processed 10 tasks in 4010 millis 处理10个任务花了4秒钟。 您会注意到仅使用了3个ForkJoinPool线程并且与并行流不同没有使用main线程。 方法4将CompletableFutures与自定义执行器一起使用 与并行流相比 CompletableFuture的优点之一是它们允许您指定其他Executor来向其提交任务。 这意味着您可以根据应用程序选择更合适的线程数。 由于我的示例不是很占用CPU因此可以选择将线程数增加到大于Runtime.getRuntime().getAvailableProcessors() 如下所示 public static void useCompletableFutureWithExecutor(ListMyTask tasks) {long start System.nanoTime();ExecutorService executor Executors.newFixedThreadPool(Math.min(tasks.size(), 10));ListCompletableFutureInteger futures tasks.stream().map(t - CompletableFuture.supplyAsync(() - t.calculate(), executor)).collect(Collectors.toList());ListInteger 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);executor.shutdown();
} 输出是 pool-1-thread-2
pool-1-thread-4
pool-1-thread-3
pool-1-thread-1
pool-1-thread-5
pool-1-thread-6
pool-1-thread-7
pool-1-thread-8
pool-1-thread-9
pool-1-thread-10
Processed 10 tasks in 1009 millis 经过改进现在仅需1秒即可处理10个任务。 如您所见 CompletableFuture s提供了对线程池大小的更多控制如果您的任务涉及I / O则应使用CompletableFuture 。 但是如果您要执行CPU密集型操作则线程数不会超过处理器没有意义因此请选择并行流因为它更易于使用。 翻译自: https://www.javacodegeeks.com/2016/06/java-8-completablefuture-vs-parallel-stream.htmljava8并行流