Skip to content

Commit

Permalink
virtual threads examples
Browse files Browse the repository at this point in the history
  • Loading branch information
rsaestrela committed Nov 25, 2023
1 parent 063a7cb commit 6ebcdca
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: build
on:
push:
branches:
- main
workflow_dispatch:
jobs:
job-container:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '21'
- id: compile-sources
run: mvn verify -pl source
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

.idea
target
2 changes: 2 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.7/apache-maven-3.8.7-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# java-virtual-threads
# Java Virtual Threads

![build-status](https://github.com/rsaestrela/java-virtual-threads/workflows/build/badge.svg)

This repository contains a few code samples of the usage of Virtual Threads, a feature described in JEP 444 and released
in Java 21.
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.estrela</groupId>
<artifactId>java-virtual-threads</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Java Virtual Threads</name>
<url>estrela.me</url>
<properties>
<java.version>21</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>21</source>
<target>21</target>
<enablePreview>true</enablePreview>
</configuration>
</plugin>
</plugins>
</build>
</project>
37 changes: 37 additions & 0 deletions src/main/java/me/estrela/jvt/MultiplePlatformThreads.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package me.estrela.jvt;

import java.time.Duration;
import java.util.concurrent.atomic.AtomicLong;

import static me.estrela.jvt.ThreadUtils.describe;

public class MultiplePlatformThreads {

public static void main(String[] args) {

AtomicLong runningThreadsCounter = new AtomicLong();

long startTime = System.currentTimeMillis();

for (int i = 1; i <= 5_000; i++) {
Thread vThread = Thread.ofPlatform()
.name("platform-thread")
.start(() -> {
runningThreadsCounter.incrementAndGet();
try {
Thread.sleep(Duration.ofMinutes(30));
} catch (InterruptedException e) {
// Let the thread die
}
});
System.out.println(describe(vThread));
if (i % 100 == 0) {
long time = System.currentTimeMillis() - startTime;
System.out.printf("%,d threads started, %,d threads running after %,d ms%n", i, runningThreadsCounter.get(), time);
}
}

}

}

26 changes: 26 additions & 0 deletions src/main/java/me/estrela/jvt/MultipleVirtualThreads.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.estrela.jvt;

import java.time.Duration;

import static me.estrela.jvt.ThreadUtils.describe;

public class MultipleVirtualThreads {

public static void main(String[] args) {
Runnable runnable = () -> {
try {
Thread.sleep(Duration.ofMinutes(30));
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
};
for (int i = 0; i < 100_000_000; i++) {
Thread vThread = Thread.ofVirtual()
.name("virtual-thread")
.start(runnable);
System.out.println(describe(vThread));
}

}

}
11 changes: 11 additions & 0 deletions src/main/java/me/estrela/jvt/ThreadUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.estrela.jvt;

public final class ThreadUtils {

public static String describe(Thread thread) {
return String.format("Thread %s-%s isVirtual=%s status=%s activeThreads=%s",
thread.getName(), thread.threadId(), thread.isVirtual(), thread.getState().name(), Thread.activeCount());
}


}
22 changes: 22 additions & 0 deletions src/main/java/me/estrela/jvt/VirtualThreadsExecutors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.estrela.jvt;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class VirtualThreadsExecutors {

public static void main(String[] args) {

try (ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor()) {
Future<Integer> future = executorService.submit(() -> 1 + 1);
Integer result = future.get();
System.out.printf("Task completed with result=%s", result);
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException(e);
}

}

}
21 changes: 21 additions & 0 deletions src/main/java/me/estrela/jvt/VirtualThreadsStarter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.estrela.jvt;

import static me.estrela.jvt.ThreadUtils.describe;

public class VirtualThreadsStarter {

public static void main(String[] args) throws InterruptedException {

Thread vThread = Thread.ofVirtual()
.name("virtual-thread")
.start(() -> System.out.println("Started Virtual Thread!"));

System.out.printf(describe(vThread));

vThread.join();

System.out.printf(describe(vThread));

}

}

0 comments on commit 6ebcdca

Please sign in to comment.