Created
October 19, 2022 04:12
-
-
Save hohonuuli/a84184b7df875e1e98e5f3fee467c1bc to your computer and use it in GitHub Desktop.
Scala/Loom example for a medium article
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
#!/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