I cannot understand why the following code locks:
"Locks around blocking futures" should "be re-entrant" in {
val lock = new Object()
def processInFuture = {
lock.synchronized {
Await.result(Future {
blocking {
logger.info("Sleeping")
Thread.sleep(100)
logger.info("Waking")
}
}, Duration.Inf)
}
}
(0 until 10).
map(_ => Future { processInFuture }).
foreach(future => Await.result(future, Duration.Inf))
}
I do not understand why creating asynchronous synchronization in a critical section causes the entire fork-join pool to work.
If I make the future executable in a separate pool from the fork connection pool, it will work. I do not understand why the fork bundle thread does not block other threads and then ends first. Is it because the pool is somehow blocked?
I understand that it is always better to do all async if its asynchronous, but some scripts do not allow this (e.g. caching with guava cache)
- EDIT
To illustrate @Dima's answer, this works
"Locks around blocking futures" should "be re-entrant" in {
val lock = new Object()
def processInFuture = {
blocking {
lock.synchronized {
Await.result(Future {
blocking {
logger.info("Sleeping")
Thread.sleep(100)
logger.info("Waking")
}
}, Duration.Inf)
}
}
}
(0 until 10).
map(_ => Future { processInFuture }).
foreach(future => Await.result(future, Duration.Inf))
}
source
share