Member-only story
Taking project Loom for a spin. Java and Scala examples
If you’re a Java developer, you’ve likely heard about project Loom. It delivers virtual threads and structured concurrency as preview features in Java 19. There are a number of articles out there with details on them, but they all leave out important details to actually get these new features running in code. Here are two brief but complete examples to help get you going. The most important bits are the following command line flags:
--enable-preview
— This flag enables preview features like Loom.--add-modules jdk.incubator.concurrent
— Without this you will get a ClassNotFound exception forStructuredTaskScope
.--source 19
— For the Java version, this is required whenever--enable-preview
is used.
Java
This example is just a single Java file that can be run using:
java --enable-preview \
--source 19 \
--add-modules jdk.incubator.concurrent \
Loom.java
Scala
This example is just a single file script that can be run if scala-cli is installed.