gpt4 book ai didi

scala - 为什么 Scala 的 Future 没有 .get/get(maxDuration) 方法,迫使我们改用 Await.result() ?

转载 作者:行者123 更新时间:2023-12-04 01:23:41 25 4
gpt4 key购买 nike

解耦get有什么特别的好处吗?方法来自 Future类(我希望它驻留的地方),而是强制编码器必须知道这个名为 Await 的外部二方法类。 ?

最佳答案

Is there any particular advantage in decoupling the get method from the Future class


是的,让开发者很难做错事。一个 Future表示将在 future 完成并且在当前调用点可能不可用的计算。如果需要阻塞一个future,为什么不同步执行呢?在线程池上调度它有什么意义,浪费一个完美的线程池线程?
documentation says :

Blocking outside the Future

As mentioned earlier, blocking on a future is strongly discouraged for the sake of performance and for the prevention of deadlocks. Callbacks and combinators on futures are a preferred way to use their results. However, blocking may be necessary in certain situations and is supported by the Futures and Promises API.


甚至 Await object documentation :

While occasionally useful, e.g. for testing, it is recommended that you avoid Await when possible in favor of callbacks and combinators like onComplete and use in for comprehensions. Await will block the thread on which it runs, and could cause performance and deadlock issues.


你可以看到语言设计者故意想要这种效果。

关于scala - 为什么 Scala 的 Future 没有 .get/get(maxDuration) 方法,迫使我们改用 Await.result() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38155159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com