Using Scala-IDE 3.0.3 (based on Scala 2.10.4), the following code completes correctly by displaying the first 10 values of the computed List
from a future as well as the Future completed
message:
import scala.concurrent._
import scala.concurrent.duration._
import scala.util.{Failure, Success}
import ExecutionContext.Implicits.global
object FutureNonBlocking extends App {
val f1: Future[List[Int]] = future {
val t = List.range(1, 50).filter(_ % 2 == 0)
println("Done")
t
}
f1.onComplete {
case Success(value) => println(value.take(10))
case Failure(e) => println("Something bad happened")
}
Await.complete(f1, 30 seconds)
}
However, changing the range List.range(1, 50)
to List.range(1, 5000)
does not display anything and (the Failure
is not triggered). Logically, it seems to be related to a memory issue but I don't understand what is happening there.
Even stranger, running this code in a REPL does not cause the issue. What am I missing there?
It turns out the whole thing is not an issue with memory management in Futures
but it is related to the way Eclipse handles console output for concurrent programs.
Adding Thread.sleep(1000)
at the end of the program let Eclipse show all the console output, solving the problem.
Thank you very much @jilen for your very helpful comment!