27

I have a Java Future object which I would like to convert into a Scala Future.

Looking at the j.u.c.Future API, there is nothing much that I could use other than the isDone method. Is this isDone method blocking?

Currently this is what I have in my mind:

val p = Promise()
if (javaFuture.isDone())
  p.success(javaFuture.get)

Is there a better way to do this?

Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
joesan
  • 13,963
  • 27
  • 95
  • 232

4 Answers4

26

Starting Scala 2.13, the standard library includes scala.jdk.FutureConverters which provides Java to Scala CompletableFuture/Future implicit conversions:

import scala.jdk.FutureConverters._

// val javaFuture = java.util.concurrent.CompletableFuture.completedFuture(12)
val scalaFuture = javaFuture.asScala
// scalaFuture: scala.concurrent.Future[Int] = Future(Success(12))
Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
  • 15
    Please note that this only works with subtypes of `java.util.concurrent.CompletionStage`. It does *not* work for `java.util.concurrent.Future`. – Daniel Bimschas Sep 05 '19 at 08:57
  • 6
    FutureConverts operates on the CompletableFuture and CompletionStage types, not the Future type. You're not answering OP's question. – K. M Sep 23 '19 at 20:45
22

How about just wrapping it (I'm assuming there's an implicit ExecutionContext here):

val scalaFuture = Future {
    javaFuture.get
}

EDIT:

A simple polling strategy could look like this (java.util.Future => F):

def pollForResult[T](f: F[T]): Future[T] = Future {
    Thread.sleep(500)
    f
  }.flatMap(f => if (f.isDone) Future { f.get } else pollForResult(f))

This will check if the Java future is done every 500ms. Obviously the total blocking time is the same as above (rounded up to the nearest 500ms) but this solution will allow other tasks to be interleaved in the same thread of the ExecutionContext.

pablochan
  • 5,625
  • 27
  • 42
  • Yeah! I already wrap it, but was just thinking if there might be a better way to do as I understand that the get operation on a Java future is a blocking operation! – joesan Jan 27 '16 at 09:31
  • Yes, but the resulting Scala `Future` is not. That is, the "wrapping" operation is non-blocking. – pablochan Jan 27 '16 at 09:33
  • 12
    This is true, but while this will not block the current thread, this still blocks a thread from the `ExecutionContext`'s pool of threads. – Régis Jean-Gilles Jan 27 '16 at 13:39
  • 2
    @RégisJean-Gilles Sure, but not knowing the context, I think this solution is fine. The only other thing that could be done is polling for the result, but that may be unnecessary here. – pablochan Jan 27 '16 at 13:47
  • 13
    It might be fine, but it's important to be clear about the thread blocking happening. That cannot be overlooked, it must be consciously assessed whether it's OK or not. It is best to be sure nobody gets the impression that wrapping a blocking call into `Future{ ... }` will magically remove any blocking. – Régis Jean-Gilles Jan 27 '16 at 14:07
  • 2
    @pablochan, how does polling help with anything? `Thread.sleep(500)` blocks the same thread in the same way as a blocking `javaFuture.get` call, doesn't it? – eddyP23 Sep 04 '17 at 14:07
  • Yes, `sleep` blocks the thread, but in a more predictable way than `javaFuture.get`. Once you call `get`, you block the thread until it's done. If the same `ExecutionContext` is used for other tasks, then a couple of long running Futures might clog it. I'm not saying that polling is objectively better than just wrapping the Future, but it might make sense in certain circumstances. – pablochan Sep 04 '17 at 18:25
  • A slight improvement would be testing if your future is a `CompletableFuture` (or `ListenableFuture` if you use Guava) in which case blocking isn't necessary. for `CompletableFuture` you can use `FutureConverters`, for `ListenableFuture` it should be simple to write an equivalent (or maybe a library exists). – Alexey Romanov Dec 27 '18 at 14:29
13

For those reading this question now, if you're using Scala 2.13 and above, use:

import scala.jdk.FutureConverters._

And convert using completableFuture.asScala

If you're using Scala 2.12 and below, use

import scala.compat.java8.FutureConverters._

And convert using: toScala(completableFuture) or completableFuture.toScala

Also, in Scala 2.12 make sure you're using the correct artifact:

org.scala-lang.modules:scala-java8-compat_2.12:0.9.0

Now, if for some reason what you have is actually a Future and not CompletableFuture, which should be a rare case nowadays, please follow first one of those answers: Transform Java Future into a CompletableFuture

ndeverge
  • 21,378
  • 4
  • 56
  • 85
Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
  • 4
    FutureConverts operates on the CompletableFuture and CompletionStage types, not the Future type. You're not answering OP's question. – K. M Sep 23 '19 at 20:44
7

Use FutureConvertors (built-in util in Scala) for conversion of Java Future to Scala Future.

Consider an example for converting Java Future[Int] to Scala Future[Int] ::

import java.util.concurrent.CompletableFuture
import scala.compat.java8.FutureConverters

val javaFuture: java.util.concurrent.Future[Int] = ??? 
// Some method call which returns Java's Future[Int]

val scalaFuture: Future[Int] = 
FutureConverters.toScala(CompletableFuture.supplyAsync(new Supplier[Int] {
      override def get(): Int = javaFuture.get 
    }))

Similar we can do for any custom type instead of Int.

  • 3
    They are good if you have a `CompletableFuture`. But the code you give is just a pointlessly more complicated version of @pablochan's answer. – Alexey Romanov Dec 27 '18 at 14:25