Last active
February 3, 2026 20:23
-
-
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/4c1aa587fcc713cbb9d356064632c3019931748a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : java futures | |
| // keywords : java, learning, futures, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // 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.4.2" | |
| //> 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