Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 27, 2023 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/78d3c1981e95c4dc9c2e116a78b2d56a to your computer and use it in GitHub Desktop.
Save dacr/78d3c1981e95c4dc9c2e116a78b2d56a to your computer and use it in GitHub Desktop.
java futures / published by https://github.com/dacr/code-examples-manager #2f285c08-14f5-4309-af23-d156994901c9/431d2e4dfbffc8849e56f6ca1d37c1c7e99d8abe
// summary : java futures
// keywords : java, learning, futures, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 2f285c08-14f5-4309-af23-d156994901c9
// created-on : 2020-09-08T21:38:26+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.3.0"
//> using dep "org.scalatest::scalatest:3.2.16"
//> using objectWrapper
// ---------------------
import org.scalatest._, flatspec._, matchers._, OptionValues._, concurrent._
import java.util.concurrent._
import java.util.function._
import scala.jdk.FutureConverters._
import scala.concurrent.{Future => ScalaFuture, Await}
import scala.concurrent.duration.Duration
object FutureTools {
// convert a java future to a scala future as future converters only work with completable future...
def javaFutureToScala[T](javaFuture: java.util.concurrent.Future[T]): scala.concurrent.Future[T] = {
val completableFuture: CompletableFuture[T] = CompletableFuture.supplyAsync(new Supplier[T] {
override def get(): T = javaFuture.get
})
completableFuture.asScala
}
// await infinitely for a java future to return its value
def awaitJavaFutureResult[T](javaFuture: java.util.concurrent.Future[T]): T = { // of course never use this ;)
Await.result(javaFutureToScala(javaFuture), Duration.Inf) // Of course never do this
}
}
class JavaLanguageAsyncBasicsTest extends AsyncFlatSpec with should.Matchers with ScalaFutures {
// ---------------------------------------------------------------------------------------
"java 5 futures" should "allow future creation (old implementation)" in {
val executor = Executors.newFixedThreadPool(4)
val javaOldFuture: FutureTask[String] = new FutureTask[String](
new Callable[String]() {
override def call(): String = "It works"
}
)
executor.execute(javaOldFuture)
ScalaFuture(javaOldFuture.get).map(result => result shouldBe "It works")
}
// ---------------------------------------------------------------------------------------
"java 8 futures" should "allow future creation (new implementation)" in {
//val executor = Executors.newFixedThreadPool(4)
val javaFuture = CompletableFuture.supplyAsync(new Supplier[String] {
override def get(): String = "It works"
})
javaFuture.asScala.map { result => result shouldBe "It works" }
}
// ---------------------------------------------------------------------------------------
it should "work with lambda expression" in {
val javaFuture =
CompletableFuture
.supplyAsync(() => "It works")
javaFuture.asScala.map { result => result shouldBe "It works" }
}
// ---------------------------------------------------------------------------------------
it should "be possible to compose intermediary results (map)" in {
val javaFuture =
CompletableFuture
.supplyAsync(() => "It works")
.handleAsync((result, error) => result.toUpperCase) // map
javaFuture.asScala.map { result => result shouldBe "IT WORKS" }
}
// ---------------------------------------------------------------------------------------
}
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[JavaLanguageAsyncBasicsTest].getName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment