The demo is a Java program which does synchronous and asynchronous threads execution. Each thread loops through exactly the same array of integers and generates a stream of pseudorandom numbers. The program calculates the time taken to perform the task synchronously and asynchronously.
Multithreading demo is comprised of two sub-projects, each built with Maven: Multithreading Demo Oversized and Multithreading Demo Improved.
The pom.xml file of each sub-project includes the Native Image Maven plugin, which instructs Maven to generate a native executable of a JAR file with all dependencies at the mvn package
step.
The plugin also generates a report using the Native Image Build Reports feature in the target directory with useful visualizations and comprehensive insights into different metrics of the native executable and the build process itself.
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.10.1</version>
<extensions>true</extensions>
<executions>
<execution>
<id>native</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<imageName>${imageName}</imageName>
<fallback>false</fallback>
<buildArgs>
<buildArg>
-H:+UnlockExperimentalVMOptions -H:+BuildReport --initialize-at-build-time
</buildArg>
</buildArgs>
<agent>
<enabled>true</enabled>
<defaultMode>Standard</defaultMode>
</agent>
</configuration>
</plugin>
-
Download and install the latest GraalVM JDK using SDKMAN!.
sdk install java 22.0.1-graal
-
Download or clone the repository and navigate into the
multithreading-demo/multithreading-demo-oversized_
directory:git clone https://github.com/graalvm/graalvm-demos
cd multithreading-demo/multithreading-demo-oversized
-
Build the project:
mvn package
-
Run the project on a JVM or as a native image:
java -jar target/multithreading-1.0-jar-with-dependencies.jar ./target/multithreading-image-oversized
Multithreading Demo Improved contains an enhanced version of the same program.
-
Change to the project directory:
cd .. multithreading-demo-improved
-
Build the project:
mvn package
-
Run the project on a JVM or as a native image:
java -jar target/multithreading-1.0-jar-with-dependencies.jar ./target/multithreading-image-improved
Learn how you can optimize a Java application by applying Profile-guided optimization (PGO) and inspect a profile using Native Image Build Reports.