Skip to content

Instantly share code, notes, and snippets.

@hohonuuli
Created October 19, 2022 04:12
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 hohonuuli/a84184b7df875e1e98e5f3fee467c1bc to your computer and use it in GitHub Desktop.
Save hohonuuli/a84184b7df875e1e98e5f3fee467c1bc to your computer and use it in GitHub Desktop.
Scala/Loom example for a medium article
#!/usr/bin/env -S scala-cli shebang --scala-version 3.2.0 -J --enable-preview -J --add-modules=jdk.incubator.concurrent
import scala.util.Using
import java.util.concurrent.ScheduledThreadPoolExecutor
import jdk.incubator.concurrent.StructuredTaskScope
Thread.startVirtualThread(() => println("Hello from virtual thread"))
println("Hello from main thread")
Using(new StructuredTaskScope.ShutdownOnFailure()) { scope =>
var aFuture = scope.fork(() => {
println("Hello from structured scope thread 1")
1
})
var bFuture = scope.fork(() => {
println("Hello from structured scope thread 2")
2
})
scope.join();
scope.throwIfFailed();
println("Hello from main thread")
val a = aFuture.resultNow()
val b = bFuture.resultNow()
println(s"a = $a, b = $b")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment