差异Await.ready和Await.result [英] Difference Await.ready and Await.result

查看:81
本文介绍了差异Await.ready和Await.result的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个悬而未决的问题,对不起.

I know this is quite an open ended question and I apologize.

我看到 Await.ready 返回 Awaitable.type ,而 Await.result 返回 T ,但是我仍然让他们感到困惑.

I can see that Await.ready returns Awaitable.type while Await.result returns T but I still confuse them.

两者之间有什么区别?

是一个阻止,另一个是非阻止吗?

Is one blocking and the other one non-blocking?

推荐答案

它们都阻塞直到将来完成,区别只是它们的返回类型.

They both block until the future completes, the difference is just their return type.

当您的 Future 引发异常时,这种区别非常有用:

The difference is useful when your Future throws exceptions:

def a = Future { Thread.sleep(2000); 100 }
def b = Future { Thread.sleep(2000); throw new NullPointerException }

Await.ready(a, Duration.Inf) // Future(Success(100))    
Await.ready(b, Duration.Inf) // Future(Failure(java.lang.NullPointerException))

Await.result(a, Duration.Inf) // 100
Await.result(b, Duration.Inf) // crash with java.lang.NullPointerException

这篇关于差异Await.ready和Await.result的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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