I am learning scala futures and I hit my question already. I have a very simple example
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
import scala.util.{Failure, Success}
/**
* Created by dummy on 05/02/15.
*/
object FutureUtils extends App{
val f = Future {
Thread.sleep(1000)
println("I am learning scala futures")
"learning"
}
f onComplete {
case Success(value:String) => println("got the response back")
case Failure(t: Throwable) => println("did not expect this")
}
println("I am still learning")
}
When I run the program as-is output never prints
got the response back
instead looks like it hangs for a minute or so and ends without printing the expected output ever. I am sure I am missing something very basic here.
I also tried adding System.in.read()
at the end and it seems when I input any dummy value, programs ends printing expected result. What is the reason behind this behavior? Could anyone please help me understand this?