Understanding scala.concurrent.Await#result

311 views
Skip to first unread message

Kevin Meredith

unread,
Nov 22, 2016, 8:44:53 AM11/22/16
to Akka User List
Given the following on Scala 2.11.8:

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global

scala> def fut = {
     |   Thread.sleep(5000)
     |   Future(42)
     | }
fut: scala.concurrent.Future[Int]

Why does the following not throw a java.util.TimeoutException?

scala> Await.result( fut, FiniteDuration(1, SECONDS) )
res1: Int = 42

As I understand, Await#result will throw if the time-out, i.e. its second argument, completes before the completion of the Future.

If that's true, then why does 42 return in the above example instead of an exception?

Kevin Meredith

unread,
Nov 22, 2016, 8:45:54 AM11/22/16
to Akka User List
Note that the following works as I had expected, per my above stated understanding:

scala> def fut = Future { Thread.sleep(5000) }
fut: scala.concurrent.Future[Unit]

scala> Await.result( fut, FiniteDuration(1, SECONDS) )
java.util.concurrent.TimeoutException: Futures timed out after [1 second]
  at scala.concurrent.impl.Promise$DefaultPromise.ready(Promise.scala:219)
  at scala.concurrent.impl.Promise$DefaultPromise.result(Promise.scala:223)
  at scala.concurrent.Await$$anonfun$result$1.apply(package.scala:190)
  at scala.concurrent.BlockContext$DefaultBlockContext$.blockOn(BlockContext.scala:53)
  at scala.concurrent.Await$.result(package.scala:190)
  ... 32 elided

Konrad Malawski

unread,
Nov 22, 2016, 8:48:05 AM11/22/16
to akka...@googlegroups.com, Kevin Meredith
Why would it – the 42 is completed immediately.
It'd be different if you'd say Future { Thread.sleep(...); 42 }
that actually makes the future take a long time.

Your first example just takes "a long time, to get to an immediately completing future.
The timeout is on the Future, not on "getting to it". :)

-- 
Konrad `ktoso` Malawski
Akka @ Lightbend
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Viktor Klang

unread,
Nov 22, 2016, 9:00:40 AM11/22/16
to Akka User List
scala> def fut = {
     |   Thread.sleep(5000)
     |   Future(42)
     | }
fut: scala.concurrent.Future[Int]

scala> Await.result( fut, FiniteDuration(1, SECONDS) )
res1: Int = 42

The code above is equivalent of:

Thread.sleep(5000)
val anon = Future(42)
Await.result(anon, FiniteDuration(1, SECONDS))


--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,
Reply all
Reply to author
Forward
0 new messages