Comparison of Java Virtual and Non-Virtual Threads in Parallel Programming
The Java programming language, in its long-term support version 21 (released in September 2023), introduced Java virtual threads (prior to version 21, this was a prototype solution). While Java non-virtual (or platform) threads are mapped one-to-one to operating system threads, multiple Java virtual threads can be executed on a single platform thread, i.e. on a single operating system thread, which makes the number of Java virtual threads practically unlimited. Java virtual threads enable writing code that is as efficient as asynchronous code but is more readable, much easier to debug, and provides clearer error messaging (exceptions) compared to asynchronous code. It is often stated that virtual threads are primarily intended for concurrent programming (e.g. each active user on an application server can now be assigned a separate virtual thread) and are less suited for parallel programming (where a task is divided into subtasks to utilize the processing power of multiprocessor systems). In this paper, we analyze the efficiency of virtual threads specifically in parallel programming (counting prime numbers that are less than a given number and the goal was not to create a particularly efficient program, but to compare different methods). First, we demonstrate the use of non-virtual Java threads in Java versions 5/6 (Executors), Java 7 (Fork Join framework), and Java 8 (Parallel Streams), and then we modify the Java 5/6 program (with very simple changes) to an equivalent program using Java 21 virtual threads. It was found that Java 21 virtual threads were slightly slower in our case than the solution using non-virtual threads.
Authors
- Siniša Sovilj (ORCID: https://orcid.org/0000-0001-7416-0152)
- Zlatko Sirotić
- Matija Oršulić
- Krešmir Pripužić
Institutions
- University of Zagreb (HR)
- Juraj Dobrila University of Pula (HR)
Publication Details
- Published
- 2025-06-02
- DOI
- https://doi.org/10.1109/mipro65660.2025.11131818
- Primary Topic
- Distributed and Parallel Computing Systems
- Type
- article
- Field-Weighted Citation Impact
- 0.00