In the following sample method implementation that uses the Scala async:
def doSomething: Future[Int] = async {
val await1 = await(someFutureResult1()) // Line 1
val await2 = await(someFutureResult2()) // Line 2
val syncCall = someFunc3() // Line 3
await1 + await2 + syncCall // Line 4
}
How is the ordering happening? The caller of doSomething returns immediately because the doSomething() is going to run in an async block. I get that. But what happens when some other thread picks up this execution and executes the contents of doSomething?
My understanding is as follows:
Line 1 starts and waits until the Future returned by someFutureResult1() is completed. Line 2 and Line 3 is not yet started!
Line 2 starts and waits until the Future returned by someFutureResult2() is completed
Line 3 starts and returns
Line 4 is called and returned
Is the flow correct?
它取决于您的“等待”的意思,发生的情况是,与Scala的普通Await.result
不同,它阻塞线程, await
将其余代码包装为Future
,并释放线程以继续在async
块外运行。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.