-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
063a7cb
commit 6ebcdca
Showing
10 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
This file contains 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
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 |
This file contains 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
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. |
This file contains 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
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> |
This file contains 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
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); | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
|
This file contains 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
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)); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains 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
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()); | ||
} | ||
|
||
|
||
} |
This file contains 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
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); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains 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
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)); | ||
|
||
} | ||
|
||
} |