What is the difference between “Future.successful(None)” and “Future(None)”

╄→гoц情女王★ 提交于 2019-12-03 14:31:26

问题


Future.apply starts an asynchronous computation whereas Future.successful creates an already completed Future with the specified result.

Now is Future(None) (Future.apply(None)) less efficient than Future.successful(None)?


回答1:


Future.apply(None) creates asynchronous computation and executes it. It means that extra lambda object is created and extra task is scheduled (however trivial task).

Future.successful(None) just produces already completed future. It is more efficient.




回答2:


I don't think that Future(None) gives a big overhead, but still in it's default implementation each call to apply spawns a new task for a ForkJoin thread pool, whereas Future.successful(None) completes immediately. And each call to map or flatMap on the future creates a new task for the poll, which also gives some overhead, so you might want to take a look at scalaz Future/Task implementation which handles such details more carefully.



来源:https://stackoverflow.com/questions/21466652/what-is-the-difference-between-future-successfulnone-and-futurenone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!