scala.concurrent.Future wrapper for java.util.concurrent.Future

前端 未结 4 1769
执念已碎
执念已碎 2020-12-06 04:36

I\'m using Play Framework 2.1.1 with an external java library that produces a java.util.concurrent.Future result. I\'m using the scala future\'s as opposed to Akka which I t

4条回答
  •  时光说笑
    2020-12-06 05:26

    import java.util.concurrent.{Future => JFuture}
    import scala.concurrent.{Future => SFuture}
    

    You can't wrap JFuture with SFuture without blocking since there is a callback in SFuture (onComplete) and there is only blocking get in JFuture.

    All you can do is to create additional thread and block it with get, then complete Promise with result of get.

    val jfuture: JFuture[T] = ???
    val promise = Promise[T]()
    new Thread(new Runnable { def run() { promise.complete(Try{ jfuture.get }) }}).start
    val future = promise.future
    

    You could check isDone in endless loop, but I don't think it is better then blocking.

提交回复
热议问题