Java 8中的completableFuture是否可扩展到多个内核? [英] Does completableFuture in Java 8 scale to multiple cores?

查看:77
本文介绍了Java 8中的completableFuture是否可扩展到多个内核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一个线程,该线程调用一堆返回completablefuture的方法,并说我将所有方法都添加到列表中,最后我执行了completablefutures.allof(list_size).join().现在列表中的期货可以扩展到多个核心吗?换句话说,期货是否安排在多个核心中以利用并行性?

Lets say I have a single thread that calls bunch of methods that return completablefuture and say I add all of them to a list and in the end I do completablefutures.allof(list_size).join(). Now does the futures in the list can scale to multiple cores? other words are the futures scheduled into multiple cores to take advantage of parallelism?

推荐答案

CompletableFuture表示与某些 ForkJoinPool.commonPool() ,默认情况下,它创建的线程数与系统具有的硬件线程数相同(通常为内核数,如果内核支持超线程,则将其数量加倍).因此,总的来说,是的,如果您使用所有默认值,那么多核将用于您可完成的期货交易.

CompletableFuture represents a task which is associated with some Executor. If you did not specify executor explicitly (for example, you used CompletableFuture.supplyAsync(Supplier) instead of CompletableFuture.supplyAsync(Supplier, Executor)), then common ForkJoinPool is used as executor. This pool could be obtained via ForkJoinPool.commonPool() and by default it creates as many threads as many hardware threads your system has (usually number of cores, double it if your cores support hyperthreading). So in general, yes, if you use all defaults, then multiple cores will be used for your completable futures.

这篇关于Java 8中的completableFuture是否可扩展到多个内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆