Scala and Java futures apparently having unexpected interactions Scala and Java futures apparently having unexpected interactions multithreading multithreading

Scala and Java futures apparently having unexpected interactions


I thought it was established somewhere that blocking is evil. Evil!

In this case, Await.result will block the current thread, because it's waiting for a result.

Await wraps the call in blocking, in an attempt to notify the thread pool that it might want to grow some threads to maintain its desired parallelism and avoid deadlock.

If the current thread is not a Scala BlockContext, then you get mere blockage.

Whatever your precise configuration, presumably you're holding onto a thread while blocked, and the thunk you're running for search wants to run something and can't because the pool is exhausted.

What's relevant is what pool produced the current Thread: whether the go-between Future is on a different pool doesn't matter if, at bottom, you need to use more threads from the current pool and it is exhausted.

Of course, that's just a guess.

It makes more sense to have a single future that gets the value from both searches, with a timeout.

But if you wind up with multiple Futures, it makes sense to use Future.sequence and wait on that.