Waiting for Loom's virtual threads...2022-04-09
Since Alan Turing time we came from running one program at a time on bare metal to running millions of java virtual threads. Which is absolutely impressive.
Why? Why should we care about threads at all? The answer is - performance. Applying concurrency can improve performance in a few ways:
- reducing latency (process one task faster)
- hiding latency (process another task while waiting)
- increasing throughput (process more tasks).
Current java concurrency implementation is based on the rule that one Java thread equals exactly one OS thread (one-to-one mapping).
This means that every
Thread.start() creates a new OS thread.
Modern high-performance JVM applications are expected to handle tens of thousands concurrent connections. And thread-per-connection architecture fails in this case.
So, there are 2 main disadvantages to the current model used in Java:
- threads are expensive to create
- threads limit can be reached very easily.
Project Loom is tackling this disadvantages. The goal of Project loom is to enable easy-to-use, high-throughput lightweight concurrency and new programming models on the Java platform. It will be done by removing involvement of the OS in the lifecycle of a virtual thread. So, large-scale JVM applications will not be restricted to a few thousand OS threads.
Loom introduces next new constructs:
- Virtual threads
Virtual thread is an implementation of threads provided by the Java runtime. They are not one-to-one wrappers over OS threads.
Virtual thread has 2 components: continuation and scheduler.
A continuation is a sequence of instructions that execute sequentially, and may suspend itself.
A scheduler assigns continuations to CPU cores, replacing a paused one with another that’s ready to run, and ensuring that a continuation that is ready to resume will eventually be assigned to a CPU core.
A carrier thread is an OS thread that on which a virtual thread executes. Over its lifetime, a single virtual thread may run on several different carrier threads.
Creating virtual threads
New syntax is not differ much from already existing threads functionality. This should not complicate adjusting of an existing code.
New method is added for creating executor service. It creates 1 virtual thread per task.
ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
And there is a builder on Thread class.
So, I am reading “Java concurrency in practice”… and hoping that Loom will be released with Java 19 this fall.