Thank you for the suggestions. It seems like the theory is true. The future still runs even when Await has timed out.
I realised that i have such poor understanding of Java concurrency. Is Scala Future using java concurrency libraries under the hood? How about libraries like cats-effect or ZIO or Monix? Do they have their own implementation of concurrency from the ground up?
If i want to understand more about all this should i pick up a book on Java concurrency? Because if libraries like cats-effect or ZIO or Monix have their own implementation from the ground up then what i will learn from Java concurrency might not be so relevant.
object Main extends App {
import concurrent.duration._
// single threaded execution context
implicit val context: ExecutionContextExecutorService = {
val executorService = Executors.newSingleThreadExecutor()
ExecutionContext.fromExecutorService(executorService)
}
val f = Future {
Thread.sleep(10.seconds.toMillis)
println("Finish sleeping")
}
try {
println(Await.result(f, 3.seconds))
} catch {
case _: Exception => println("Caught exception")
}
finally
{
println("Finished Await")
context.shutdown()
}
println("At main")
}
Output
Caught exception
Finished Await
At main
//after 10 seconds
Finish sleeping