scala Await.ready 和 Await.result 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/41170280/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference Await.ready and Await.result
提问by octavian
I know this is quite an open ended question and I apologize.
我知道这是一个非常开放的问题,我深表歉意。
I can see that Await.readyreturns Awaitable.typewhile Await.resultreturns Tbut I still confuse them.
我可以看到Await.ready返回Awaitable.type时Await.result返回T但我仍然混淆它们。
What are the difference between the two?
两者有什么区别?
Is one blocking and the other one non-blocking?
一个是阻塞的,另一个是非阻塞的吗?
回答by soote
They both block until the future completes, the difference is just their return type.
它们都阻塞直到未来完成,区别只是它们的返回类型。
The difference is useful when your Futurethrows exceptions:
当您Future抛出异常时,差异很有用:
def a = Future { Thread.sleep(2000); 100 }
def b = Future { Thread.sleep(2000); throw new NullPointerException }
Await.ready(a, Duration.Inf) // Success(100)
Await.ready(b, Duration.Inf) // Failure(java.lang.NullPointerException)
Await.result(a, Duration.Inf) // 100
Await.result(b, Duration.Inf) // crash with java.lang.NullPointerException
回答by lex82
Both are blocking for at most the given Duration. However, Await.resulttries to return the future result right away and throws an exception if the future failed while Await.readyreturns the completed future from which the result (Successor Failure) can safely be extracted via the valueproperty. 
两者都阻塞最多给定的Duration. 但是,Await.result尝试立即返回未来结果并在未来失败时抛出异常,同时Await.ready返回可以通过属性安全地从中提取结果(Success或Failure)的完整未来value。
The latter is very handy when you have to deal with a timeout as well:
当您还必须处理超时时,后者非常方便:
val future = Future { Thread.sleep(Random.nextInt(2000)); 123 }
Try(Await.ready(future, 1.second)) match {
    case Success(f) => f.value.get match {
      case Success(res) => // handle future success 
      case Failure(e) => // handle future failure
    }
    case Failure(_) => // handle timeout
}
When using Await.result, the timeout exception and exceptions from failing futures are "mixed up".
使用时Await.result,超时异常和期货失败的异常“混在一起”。
Try(Await.result(future, 1.second)) match {
    case Success(res) => // we can deal with the result directly
    case Failure(e) => // but we might have to figure out if a timeout happened
}
回答by λ Allquantor λ
In general, both are blocking.
一般来说,两者都是阻塞的。
The difference is that Await.readyis blocking until the Future has finished (successful or failed) in given time. 
不同之处在于Await.ready阻塞直到 Future 在给定时间内完成(成功或失败)。
The only one difference is that readyblocks until the Awaitableis ready and the resultdoes yield the result type T.
唯一的区别是ready阻塞直到Awaitable准备好并且result确实产生结果类型T。
Postscriptum:In practice, if you want to perform some actions like error checking or logging you would take Await.ready(...)if you want to compose the result and throw an error if something goes wrong take Await.result(...).
后记:在实践中,如果您想执行一些操作,例如错误检查或日志记录,Await.ready(...)如果您想组合结果并在出现问题时抛出错误,您将采取Await.result(...)。
As rule of thumb - try to avoid Await.
根据经验 - 尽量避免等待。

