This repository contains a few selected samples to illustrate behavioral differences between Java Platform Threads and Virtual Threads.
You need at least Java 19 to run these samples and you need Postgres if you want to run the Postgres-based samples.
This repository contains three types of samples:
-
These illustrate differences in concurrency behavior using
synchronized
vsLock
s. Seeloomdemo.standalone
packages for the individual runnable classes. -
The Spring Boot examples reside in
loomdemo.springboot
and its subpackages. The examples demonstrate how to configure Tomcat and Spring’sAsyncTaskExecutor
to use Virtual threads. -
Benchmarks measure Virtual vs. Platform thread performance, specifically the throughput of creating and running a
Runnable
on a Virtual vs. Platform thread. SeeVirtualThreadsBenchmark.runAndAwait
and ThreadPoolExecutorBenchmark.runAndAwait` benchmark. The difference in the score is an approximation for the Virtual threads overhead in comparison to pooled Platform threads.Benchmarks are located in the
test
sources and use Microbenchmark Runner to run benchmarks through Surefire/the JUnit 5 engine.
You can use docker compose to run the database if you like:
$ docker-compose up
...
postgres_1 | 2022-09-27 11:54:59.006 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2022-09-27 11:54:59.042 UTC [59] LOG: database system was shut down at 2022-09-27 11:54:58 UTC
postgres_1 | 2022-09-27 11:54:59.060 UTC [1] LOG: database system is ready to accept connections
There are devcontainers configuration files provided so you can also use those to install the JDK. Install the Remote Containers extension in VSCode, open the command palette and execute >Remote-Containers: Reopen in container
, or open the project in Codespaces.
If you want to try build this project, then use the bundled the maven wrapper. You also need JDK 19 or above.
$ ./mvnw clean install
If you want to build with the regular mvn
command, you will need Maven v3.5.0 or above.
For an earlier iteration on Spring Boot and Virtual Threads see the https://github.com/mp911de/spring-boot-virtual-threads-experiment repository that contains a Spring Boot example only.
This project is Open Source software released under the Apache 2.0 license.