This answer instructs how to convert java.util.concurrent.Future
into scala.concurrent.Future
, while managing where the blocking will occur:
import java.util.concurrent.{Future => JFuture}
import scala.concurrent.{Future => SFuture}
val jfuture: JFuture[T] = ???
val promise = Promise[T]()
new Thread(
new Runnable {
def run() { promise.complete(Try{ jfuture.get }) }
}
).start
val future = promise.future
My queston is the same as a question asked in the comments:
what's wrong with
future { jfuture.get }
? Why you used an extra thread combined with Promise?
It was answered as follows:
it'll block thread in your thread pull. If you have a configured ExecutionContext for such futures it's fine, but default ExecutionContext contains as many threads as you have processors.
I'm not sure I understand the explanation. To reiterate:
What's wrong with future { jfuture.get }
? Isn't blocking inside a future the same as manually creating a new Thread and blocking there? If not, how is it different?