From what I understand, a Future will be handled by a new thread from the thread pool. Meanwhile, the main thread can continue to do its computation that does not depend on the result of the Future.
This, from https://www.playframework.com/documentation/2.4.x/ThreadPools, supports my understanding.
Note that you may be tempted to therefore wrap your blocking code in Futures. This does not make it non-blocking, it just means the blocking will happen in a different thread. You still need to make sure that the thread pool that you are using has enough threads to handle the blocking.
In addition, in JVM, there is no concept of parent-children among threads. In other words, all threads are treated equally. That means, the thread addressing Future will continue to be running even the main thread finishes.
However, in the following example, why when the main thread finishes, the Future thread is killed as well. If I do not comment out sleep(10000) at the end of the example, I can see the Future result.
val f3 = Future {
sleep(5000)
2
}
f3.onComplete {
case Success(value) => println(s"f3's result = $value")
case Failure(e) => e.printStackTrace
}
// do other work
println("A ...")
sleep(100)
println("B ...")
sleep(100)
println("C ...")
sleep(100)
println("D ...")
sleep(100)
println("E ...")
sleep(100)
println("F ...")
sleep(100)
// keep the JVM running
// sleep(10000)
def sleep(duration: Long) {
Thread.sleep(duration)
}