The purpose of this repository to is to demonstrate running virtual threads under Java 21. The repository contains two Maven projects.
One project creates and runs threads under the Java 11 SDL. The other creates and runs virtual threads under Java 21.
Or course both the Java 11 and Java 21 projects can be run on the same machine by installing both the Java 11 and Java 21 SDKs on the same machine and configuring the projects according to the specific SDK version.
However, the easier way is to create to separate virtual machines installing Java 11 and Maven 3.9.4 on one machine and Java 21 and Maven 3.9.4 on the other virtual machine.
Another way is to run the specific SDK under a Docker container. Thus, you'll install two container images, one for Java 11 and the other image for Java 21. Then you'll run Docker containers according to those images respectively.
Both the dedicated virtual machine technique and the technique of running each SDK as a Docker container are described in the following sections.
The following sections describe how to run the demonstration projects on dedicated virtual machines running Ubuntu. These instructions assume that you've created two virtual machines.
Once the virtual machines are created, install Java 11 on one of the machines using the instructions shown below.
sudo apt update -y
sudo apt install openjdk-11-jdk -y
java --version
On the other virtual machine install Java 21 using the instructions shown below.
sudo apt update -y
sudo apt-get install wget -y
wget https://download.java.net/java/GA/jdk21/fd2272bbf8e04c3dbaee13770090416c/35/GPL/openjdk-21_linux-x64_bin.tar.gz
tar -xvf openjdk-21_linux-x64_bin.tar.gz
cd jdk-21
sudo mkdir -p /usr/local/jdk-21
sudo mv * /usr/local/jdk-21
echo 'export JAVA_HOME=/usr/local/jdk-21' >> ~/.bashrc
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
java --version
Install Maven 3.9.4 on both virtual machines using the instructions shown below.
cd ~/
wget https://dlcdn.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz
sudo tar xzvf apache-maven-3.9.4-bin.tar.gz -C/opt/
echo 'export MAVEN_HOME=/opt/apache-maven-3.9.4' >> ~/.bashrc
echo 'export PATH=$MAVEN_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
mvn --version
Install the source code on both virtual machines using the following instructions.
cd ~/
git clone https://github.com/reselbob/SimpleVirtualThreads.git
cd SimpleVirtualThreads
On the virtual machine running Java 11 go to the directory that has the source code for the Java 11 here and execute the following instructions.
cd Java11
mvn compile
mvn exec:java -Dexec.mainClass="org.example.App"
You'll see error ouput similar to the following:
There is insufficient memory for the Java Runtime Environment to continue.
On the virtual machine running Java 21 go to the directory that has the source code for the Java 21 here and execute the following instructions.
cd Java21
mvn compile
mvn exec:java -Dexec.mainClass="org.example.App"
Upon success you'll see the following output:
All threads under have run under Java 21.
As mentioned above, you can run both the Java 11 and Java 21 code as Docker containers. The instructions for running containers for each Java SDK version follows.
Run the following instructions to run the Java 11 source code from within a Docker container. Be advised that the Java 11 source code is installed within the Docker image.
cd Java11
Create the Docker image:
docker build -t my-java-11-app -f Dockerfile .
Run the Docker container for Java 11 based on the image created above.
docker run -it --rm -a stdout -a stderr my-java-11-app mvn -f /app/SimpleVirtualThreads/Java11/pom.xml exec:java -Dexec.mainClass="org.example.App"
Run the following instructions to run the Java 21 source code from within a Docker container. Be advised that the Java 21 source code is installed within the Docker image.
cd Java21
Create the Docker image:
docker build -t my-java-21-app -f Dockerfile .
Run the Docker container for Java 21 based on the image created above.
docker run -it --rm -a stdout -a stderr my-java-21-app mvn -f /app/SimpleVirtualThreads/Java21/pom.xml exec:java -Dexec.mainClass="org.example.App"