Waardemeesters

Virtual threads do not require or expect application code to explicitly hand control back to the scheduler; in other words, virtual threads are not cooperative. User code must not make assumptions about how or when virtual threads are assigned to platform threads any more than it makes assumptions about how or when platform threads are assigned to processor cores. It is no longer possible, as it was in the preview releases, to create virtual threads that cannot have thread-local variables. Guaranteed support for thread-local variables ensures that many more existing libraries can be used unchanged with virtual threads, and helps with the migration of task-oriented code to use virtual threads.

The change to GetAllThreads to return an array containing only the platform threads may be an issue for some agents. Existing agents that enable the ThreadStart and ThreadEnd events may encounter performance issues since they lack the ability to limit these events to platform threads. The JDK’s virtual thread scheduler is a work-stealing ForkJoinPool that operates in FIFO mode.

Java Thread Model and Virtual Threads

The reactive programming initiatives try to overcome the lack of thread resources by building a custom DSL to declaratively describe the data flow and let the framework handle concurrency. However, DSL is tough to understand and use, losing the simplicity Java tries to give us. By default, Quarkus runs services with a reactive approach, thus it is necessary to configure the service with the fine-grained setup using @Blocking, @NonBlocking, and @UseVirtualThreads annotations.

java virtual threads example

As we can see, the IO operation, the sleep() method, is after the infinite loop. We also defined an alwaysTrue() function, which returns true and allows us to write an infinite loop without using the while construct that is not permitted by the compiler. If you do a big rewrite of your codebase, you might end up in a situation where a part is rewritten and other parts aren’t. Your personal data collected in this form will be used only to contact you and talk about your project. I think that programmers won’t use Loom directly on a daily basis. It is more about when the vast number of libraries and frameworks will implement support for Loom.

ThreadLocal and Thread Pools

Virtual threads, on the other hand, are managed entirely by the JVM, so context switching is much faster and less expensive. Let’s first understand the thread-per-request https://globalcloudteam.com/ model and how threads work in Java. For JSON serialization, Netty uses their custom implementation of thread locals, FastThreadLocal to store buffers.

java virtual threads example

The problem with platform threads is that they are expensive from a lot of points of view. Whenever a platform thread is made, the OS must allocate a large amount of memory in the stack to store the thread context, native, and Java call stacks. Moreover, whenever the scheduler preempts a thread from execution, this enormous amount of memory must be moved around. You can try to migrate it top-down – you make sure that the calling code is executed in virtual threads and rewrite that step by step to block. For that, you could run some reactive operation and block for the final result of the operation.

History: Why We Need Virtual Threads

Assumptions leading to the asynchronous Servlet API are subject to be invalidated with the introduction of Virtual Threads. The async Servlet API was introduced to release server threads so the server could continue serving requests while a worker thread continues working on the request. It’s available since Java 19 in September 2022 as a preview feature. Its goal is to dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications. In the second variant, Thread.ofVirtual() returns a VirtualThreadBuilder whose start() method starts a virtual thread. The alternative method Thread.ofPlatform() returns a PlatformThreadBuilder via which we can start a platform thread.

Generate the project, open it in IntelliJ, and make sure the pom.xml file has maven-compiler-plugin configuration setup to enable the preview feature. Below is the FortuneRepository except the blocking we’ve seen earlier has been replaced by reactive methods. The vertx-sql-client is a reactive client, hence it is not supposed to block while waiting for the completion of a transaction with the database.

One comment on “Virtual Threads in Java (Project Loom)”

One last thing that we wanted to mention is that virtual threads also opens the door to introduce structured concurrency in Java. This change will also make Java code much safer when running multiple concurrent tasks at different nested levels. Most JVM implementations today implement Java threads as thin wrappers around operating system threads.

java virtual threads example