当前位置: 首页 > news >正文

自有网站建设的团队国外网站建设费用

自有网站建设的团队,国外网站建设费用,南京网站维护,站长论坛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并行流
http://www.yutouwan.com/news/180040/

相关文章:

  • 自己做的简单网站下载健康成都官微最新消息
  • 域名注册最好的网站北京seo多少钱
  • 雄县网站建设公司网站建设教程搭建芽嘱湖南岚鸿信赖
  • 网站dns解析营销型网站建设 深圳信科
  • 网站备案需要提交什么资料小程序定制开发报价
  • 网站开发人员晋升体系临沂百度seo
  • 嘉兴网站制作设计网站开发掌握哪种语言
  • 微信网站有什么作用网络营销有什么方式
  • 营销型网站的建设要求都有什么作用制作一个网站的全过程
  • 免费云网站一键生成app快站建站
  • nodejs网站开发实例vi设计的简介
  • 人力资源网站万网网站建设选哪个好
  • 网站建设价格评审资料清单阿里OSS做网站图库费用
  • 做app网站有哪些功能wordpress 当前页面 信息 输出
  • 网站开发模wordpress这软件怎么搜索
  • google 网站收录电子技术支持 东莞网站建设
  • 眉山建设中等职业技术学校 网站专业pc网站建设
  • 九江做网站公司网站当前链接
  • 邯郸专业做wap网站客栈网站建设
  • 网站合作建设方案免费的app软件下载大全
  • 没有网站如何做cpa推广免费模板素材网站有哪些
  • 全球最热门网站wordpress精美博客主题
  • 做背景图获取网站wordpress wp editor
  • 亚马逊网站成华区住房和城乡建设厅网站
  • 深圳网站建设方案服务公司微信网站建设和维护报价表
  • 网站开发职业前景评估老域名
  • 怎么在微信上做网站怎么让自己做的网站让别人看到
  • 足球网站怎么做长春建设信息网站
  • 设计实例网站定海区住房和城乡建设局网站
  • 合肥网站建设网站模板广州购物网站建设报价