DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Virtual Threads: A Game-Changer for Concurrency
  • Java Thread Dump Analysis
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • The Energy Efficiency of JVMs and the Role of GraalVM

Trending

  • Medallion Architecture: Why You Need It and How To Implement It With ClickHouse
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • How to Convert XLS to XLSX in Java
  • Beyond Simple Responses: Building Truly Conversational LLM Chatbots
  1. DZone
  2. Coding
  3. Java
  4. Java Virtual Threads and Scaling

Java Virtual Threads and Scaling

Learn how Java Virtual Threads, introduced in JDK 19 and fully supported in JDK 21, simplify concurrency with lightweight threads and enhance scalability.

By 
Hitesh Jodhavat user avatar
Hitesh Jodhavat
·
May. 05, 25 · Analysis
Likes (0)
Comment
Save
Tweet
Share
3.1K Views

Join the DZone community and get the full member experience.

Join For Free

Java Virtual Threads were introduced in JDK 19 as a preview feature as part of Project Loom, but they are fully supported starting from JDK 21 and beyond (JDK 24 includes further refinements). 

Virtual threads primarily help developers by simplifying concurrent programming — eliminating the need to choose between synchronous and reactive models, and reducing the complexity of working with NIO (non-blocking I/O) APIs. They seamlessly translate traditionally blocking operations such as I/O calls, synchronization, and thread sleeping into efficient, non-blocking operations under the hood, while allowing developers to write code in a straightforward, blocking style.

Traditional Threading Model

In traditional multithreaded Java applications, the container creates N Java threads, with each Java thread mapped directly to an underlying OS thread. The operating system uses time-slicing to schedule these OS threads for execution on a limited number of CPU cores. When a thread performs a blocking operation, such as an I/O call, acquiring a lock (synchronization), or calling sleep(), the associated OS thread is taken off the CPU, and another thread is scheduled in its place. 

However, when there are too many threads competing for CPU time, excessive context switching occurs, leading to performance degradation due to the overhead of constantly saving and restoring thread states. 

Traditional Threading Model


Java Virtual Threading Model

In the Java Virtual Threading model, the JVM utilizes a small number of carrier threads, typically equal to the number of available CPU cores. Each carrier thread is backed by a single OS thread, meaning that there is minimal context switching at the OS level. Instead of relying on the operating system, the JVM itself handles the scheduling and context switching of virtual threads entirely in user space.

When a Java application creates N virtual threads, the JVM efficiently maps them onto the available carrier threads. These virtual threads are extremely lightweight compared to traditional platform threads, allowing the application to spawn thousands (or even millions) of them without significant resource overhead. The JVM manages the lifecycle and execution of virtual threads internally, suspending and resuming them as needed without involving the OS scheduler, which greatly enhances scalability and performance for concurrent applications.

Java Virtual Threading Model


Thread Pool and CPU Scheduling

In a typical business application, workloads generally consist of both CPU-bound operations (such as calculations or data processing) and I/O-bound operations (like remote service calls or database interactions) to support the required functionality.

To achieve higher concurrency and better utilization of system resources, applications often define a thread pool with a size greater than the number of available CPU cores. This allows threads that are blocked on I/O to give way to others that can continue executing, ensuring the CPUs remain busy.

Now, consider a CPU-bound business operation, such as computing the factorial of large numbers. Suppose we need to complete 20 such operations, each taking approximately 1 second to execute on a machine with 4 CPU cores.

Traditional Threads

With traditional threads, the operating system uses time-slicing to share CPU cores among multiple threads. In this case, if we create 20 threads to perform 20 CPU-bound operations on a 4-core machine, the OS will schedule them in a round-robin fashion, giving each thread a time slice (e.g., 100ms) on the available cores.

As a result, instead of processing four operations at a time (as in an ideal parallel execution scenario), all 20 threads will be interleaved across the CPU cores. Each thread gets a short time slice, waits, and then gets scheduled again. This continues until each operation completes. 

Due to the constant context switching and shared CPU time, each individual operation will still take approximately 5 seconds to complete, even though all operations may appear to finish around the same time. This leads to increased latency per operation and inefficient CPU utilization in CPU-bound scenarios.

Traditional Threads


Virtual Threads

Virtual threads do not rely on OS-level time slicing. Once a virtual thread is mounted on a carrier thread, it continues executing uninterrupted until it hits a blocking operation (e.g., I/O, synchronization). In a purely CPU-bound workload, like computing factorials, each virtual thread will keep running as long as it has CPU time available.

On a 4-core machine, this means that only four virtual threads will run at a time, each fully utilizing a CPU core. The first four operations will complete in approximately 1 second, the next 4 in 2 seconds, and so on. By the end of 5 seconds, all 20 operations will be completed.

The key distinction here is that, while the total execution time remains the same as with traditional threads (because we’re limited by CPU capacity), individual operations finish sooner, and context switching overhead is reduced. This results in more predictable performance and better CPU utilization, especially when mixing CPU-bound and I/O-bound workloads.

Virtual Threads


CPU-Bound Operation Processing Time Comparison

Below are the results of (2000000000!) compute factorial operation on 8 CPU AMD EPYC 9J14 96-Core Processor, CPU MHz 2596. 200 operations computed.

  • Each operation (2000000000!) takes ~1.68s.
  • Traditional thread with 200 pool size takes an average of 35s for each operation.
  • Virtual thread takes ~1.68s.
ThreadPool ThreadPool - 8 ThreadPool - 16 ThreadPool - 24 ThreadPool - 200 VirtualThread
Individual operation time 1669 1662 3262 3138 4767 4841 30992 31979 1669 1674
1670 1669 3292 3217 4770 4893 31810 32219 1681 1675
1670 1670 3287 3269 4815 4926 32358 32361 1682 1677
1670 1670 3294 3285 4839 4938 32889 32505 1684 1676
1671 1671 3306 3248 4850 4927 32969 32527 1685 1677
1672 1672 3322 3327 4872 4930 33194 32837 1684 1683
1677 1676 3307 3334 4946 4931 33220 33053 1685 1687
1677 1675 3326 3359 4957 4944 33321 33531 1685 1689
1645 1660 3347 3347 4933 4965 33310 33508 1678 1678
1645 1670 3330 3376 4934 4983 33475 33784 1677 1679
1647 1684 3334 3318 4972 4925 33479 33725 1679 1678
1650 1682 3336 3364 4960 4928 33846 33736 1679 1679
1660 1684 3322 3382 4959 4996 34021 33929 1678 1677
1661 1682 3345 3409 4980 5011 34017 34022 1679 1689
1661 1682 3320 3340 4962 4975 34208 33890 1679 1678
1667 1689 3341 3356 4964 4992 34257 34044 1689 1681
1681 1680 3354 3359 4997 4988 34583 34687 1663 1661
1681 1691 3347 3264 5017 4973 34893 34791 1681 1663
1683 1684 3349 3379 5002 4949 34873 34891 1682 1661
1684 1681 3329 3307 5008 5025 35041 35110 1682 1681
1682 1686 3347 3365 5005 5014 35309 35147 1672 1673
1681 1684 3377 3368 5004 4990 35010 35209 1682 1681
1691 1689 3338 3341 5013 5037 35187 35452 1683 1680
1681 1683 3346 3372 5000 5039 35399 35724 1682 1681
1680 1681 3331 3378 4889 4777 35414 35438 1681 1681
1683 1682 3379 3387 5009 4830 35615 35601 1682 1682
1684 1682 3370 3362 5000 4824 35566 35622 1681 1683
1681 1683 3359 3361 4994 4868 35677 35933 1681 1681
1681 1691 3349 3357 5040 5023 35551 35859 1682 1681
1682 1683 3361 3383 5043 5112 36007 35790 1686 1680
1681 1691 3358 3392 4910 5046 35910 36049 1687 1681
1693 1693 3360 3361 5052 5058 36190 35947 1688 1692
1681 1681 3288 3380 5068 5007 36094 36208 1685 1686
1681 1682 3354 3369 5082 5013 35983 36303 1681 1693
1681 1682 3322 3358 5048 5132 36062 36468 1682 1682
1687 1683 3365 3366 5100 5092 36022 36100 1683 1681
1682 1682 3349 3374 5045 5058 36208 36191 1689 1681
1682 1683 3360 3368 5100 5069 35967 36426 1682 1680
1682 1692 3349 3326 5059 5058 36222 36760 1683 1683
1692 1685 3363 3361 5140 5130 36206 36785 1683 1684
1681 1686 3371 3369 5026 5080 36493 36671 1685 1682
1682 1681 3391 3372 5009 5124 36699 37041 1683 1686
1685 1682 3366 3367 4947 5039 36524 36925 1683 1681
1681 1682 3346 3313 5006 5015 36495 36999 1682 1681
1682 1682 3351 3365 5060 5046 36598 36974 1686 1690
1682 1694 3362 3371 5051 5073 36927 36830 1682 1682
1683 1683 3354 3376 5014 5070 36561 37216 1686 1683
1693 1686 3386 3371 5117 5061 36629 36723 1690 1686
1681 1680 3347 3367 5049 5006 37137 36981 1697 1681
1681 1682 3345 3353 4878 5055 37065 37041 1681 1681
1683 1682 3359 3371 5032 5057 36980 36509 1682 1680
1681 1690 3360 3354 5050 5058 36995 36896 1683 1681
1685 1682 3355 3366 5053 4947 37308 37302 1682 1683
1682 1684 3362 3368 5124 4977 37069 37173 1682 1680
1682 1685 3359 3347 5041 5029 37089 37555 1681 1688
1693 1688 3363 3354 5050 5074 37607 37330 1684 1692
1681 1680 3344 3373 5028 5060 37216 37062 1682 1680
1683 1682 3382 3361 5043 5034 37664 37413 1693 1681
1682 1682 3376 3364 5068 5033 36934 37147 1682 1680
1684 1682 3370 3347 5052 5048 37405 37761 1681 1682
1682 1691 3347 3363 5052 5060 37082 37535 1683 1680
1690 1682 3374 3356 5024 5065 37480 37172 1681 1681
1682 1684 3358 3379 5049 5075 37694 37925 1684 1683
1682 1682 3397 3354 5107 5085 37645 37753 1682 1692
1682 1681 3369 3167 5042 5053 37768 37427 1683 1682
1682 1683 3357 3296 5047 5069 37938 37218 1681 1681
1682 1681 3362 3348 5106 5034 37554 38041 1682 1682
1689 1683 3354 3364 5147 5047 37952 37369 1683 1681
1687 1685 3336 3404 5052 5049 37608 38024 1682 1682
1683 1684 3352 3412 5049 5067 37532 38011 1682 1684
1682 1692 3360 3360 5080 5054 37687 38309 1693 1682
1687 1684 3349 3377 5090 5071 38119 37486 1684 1692
1681 1681 3378 3350 4400 4309 37602 37849 1683 1680
1682 1681 3369 3342 5110 4819 37591 38476 1681 1680
1681 1682 3362 3428 5076 5069 37618 37684 1681 1681
1681 1683 3361 3456 5071 5144 38112 38250 1683 1681
1682 1682 3361 3394 5057 5100 37632 38071 1682 1684
1690 1683 3358 3375 5122 4763 37338 38288 1681 1684
1681 1683 3350 3377 5115 5028 37655 38349 1682 1682
1683 1691 3380 3426 5100 4957 38358 37569 1694 1692
1681 1681 3363 3365 5125 5156 38272 38315 1686 1681
1684 1682 3359 3357 5121 5138 38439 37843 1681 1681
1684 1682 3342 3358 5224 5097 37827 38237 1681 1684
1682 1682 3387 3378 5071 5093 37648 37970 1684 1683
1682 1685 3328 3372 5084 5192 37824 36821 1683 1685
1682 1684 3358 3367 5124 5135 38411 34984 1683 1684
1697 1685 3352 3327 5167 5155 38032 38040 1682 1699
1682 1692 3353 3356 5133 5026 37884 38819 1707 1694
1681 1681 3352 3380 5183 5091 37800 37332 1681 1680
1682 1682 3371 3370 5092 5143 37558 38393 1681 1681
1681 1681 3351 3363 5138 5112 38019 37724 1683 1680
1682 1682 3376 3356 5066 5116 37710 38132 1682 1682
1682 1683 3355 3364 5219 5177 37617 38549 1683 1682
1681 1684 3363 3381 5207 5131 37935 37830 1681 1682
1691 1682 3380 3363 5099 5130 38567 38212 1681 1682
1683 1692 3366 3396 5220 5261 38085 38977 1692 1693
1681 1681 3370 3360 5047 5041 38431 37926 1681 1680
1682 1682 3379 3356 5045 5060 37832 37969 1680 1681
1683 1683 3366 3366 5044 5053 38586 37477 1681 1681
1682 1686 3356 3375 5000 4979 37386 37522 1681 1682
1682 1686 3377 3349 5062 5067 38011 38581 1682 1692
1682 1694 3355 3232 5050 5059 38588 38314 1697 1683
1694 1722 3353 3360 5021 5027 37701 38329 1688 1737
1684 1705 3357 3359 5036 5039 37882 39015 1736 1692
1681 1682 3360 3312 5060 5059 38591 37149 1680 1681
1682 1681 3360 3347 5105 5033 37870 38672 1681 1681
1681 1682 3356 3391 5037 5044 37822 37468 1682 1683
1682 1682 3313 3400 5045 5061 37183 38254 1681 1683
1682 1685 3347 3391 5011 5034 37322 38297 1682 1682
1681 1682 3367 3352 5020 5027 37693 37913 1682 1682
1692 1682 3371 3361 5027 5020 37630 38135 1690 1681
1682 1691 3348 3375 5040 5043 38449 38258 1682 1695
1683 1681 3304 3379 5036 5060 37944 38365 1684 1719
1684 1682 3362 3396 5026 5034 37712 38484 1685 1719
1686 1682 3368 3403 5027 5030 37366 38537 1687 1723
1683 1685 3361 3308 5023 5002 38379 38130 1685 1723
1684 1686 3364 3230 5041 5018 38645 38939 1699 1739
1684 1683 3359 3377 5070 5037 38341 39027 1681 1685
1689 1684 3364 3380 5030 5045 37031 37102 1698 1735
1702 1695 3372 3337 5052 5032 37476 37624 1688 1722
1681 1682 3376 3367 5067 5027 37710 38569 1681 1681
1681 1684 3361 3392 5039 5017 37470 36878 1681 1680
1680 1682 3379 3390 5000 5077 37289 35580 1682 1682
1682 1683 3405 3373 4996 5010 37464 37068 1687 1684
1683 1685 3379 3423 5057 4976 38334 37358 1684 1682
1681 1690 3362 3364 5061 5052 37639 38340 1682 1691
1681 1684 3360 3414 5047 5038 38335 37607 1688 1683
1691 1684 3360 3368 5036 5064 37527 36373 1681 1683
1682 1683 2898 3356 5087 5071 36833 37953 1682 1680
1696 1683 3071 3375 5023 5037 38167 37746 1682 1681
1683 1683 3387 3373 5042 5046 37216 38224 1682 1683
1682 1688 3201 3365 5019 5031 37305 37540 1682 1693
1683 1685 3392 3361 5049 5021 38477 37463 1682 1681
1722 1685 3401 3349 5054 5037 37342 38214 1681 1682
1695 1685 3431 3350 5049 5036 37494 38566 1692 1683
1692 1692 3470 3357 5035 5069 38420 37728 1683 1684
1681 1721 3417 3350 5030 5068 38297 37948 1681 1681
1682 1721 3408 3350 5034 5052 37645 38241 1682 1680
1682 1720 3441 3379 5027 5063 38214 38215 1681 1682
1682 1719 3476 3356 5038 5018 37111 37122 1682 1682
1680 1719 3462 3378 5038 5031 37910 38622 1681 1683
1688 1721 3524 3363 5079 5059 37280 37225 1681 1692
1694 1720 3403 3366 5055 5058 37106 37904 1692 1682
1689 1731 3525 3359 5058 5044 37077 38150 1710 1683
1682 1682 3367 3358 5052 5083 37064 37519 1703 1681
1681 1683 3370 3353 5028 5011 36974 38627 1729 1680
1683 1683 3359 3356 5029 5011 36914 35098 1703 1682
1683 1682 3359 3361 5047 5029 36859 37193 1732 1681
1692 1684 3362 3364 5040 5046 37047 38725 1722 1682
1684 1683 3367 3365 5081 5033 37237 37578 1737 1698
1696 1692 3356 3361 5036 5065 36417 36476 1716 1682
1684 1690 3355 3363 5027 5061 36533 36347 1689 1684
1681 1683 3371 3349 5086 5047 36754 38752 1681 1680
1682 1682 3367 3359 5038 5036 36642 35374 1680 1680
1682 1682 3363 3381 5046 5041 36525 36487 1683 1683
1680 1682 3372 3366 5037 5032 37606 36831 1681 1681
1682 1683 3356 3352 5040 5051 36747 37367 1682 1687
1682 1684 3368 3369 5065 5035 35692 36123 1682 1684
1682 1692 3359 3372 5010 5018 36387 34970 1682 1682
1693 1684 3366 3360 5032 5056 37703 37929 1692 1691
1681 1687 3395 3354 5037 5037 36957 36601 1694 1682
1681 1687 3368 3354 5038 5036 36922 37681 1681 1681
1681 1687 3282 3364 5042 5036 36388 37456 1682 1682
1681 1688 3358 3320 5043 5034 35950 37250 1682 1693
1682 1685 3351 3359 5030 5024 36338 36840 1694 1682
1682 1683 3367 3376 5064 5048 35374 37226 1688 1691
1688 1697 3389 3378 5047 5044 36554 36967 1689 1684
1687 1690 3343 3369 5048 5043 35601 37038 1698 1696
1681 1682 3315 3350 5022 5034 35756 36398 1687 1682
1681 1683 3390 3285 5056 5036 35528 36842 1681 1680
1680 1686 3357 3373 5038 5050 35207 34960 1681 1683
1680 1682 3358 3376 5063 5052 35776 35364 1681 1681
1682 1684 3366 3368 5033 5068 35089 35097 1687 1682
1682 1684 3379 3365 5033 5031 35168 36345 1682 1687
1682 1688 3397 3366 5068 5068 35033 35743 1686 1682
1691 1685 3358 3388 5035 5058 35482 37499 1689 1691
1681 1682 3362 3357 5028 5064 34894 36168 1680 1681
1681 1682 3350 3361 5046 5037 35710 36455 1681 1680
1680 1682 3383 3376 5042 5041 34981 34961 1682 1682
1682 1683 3345 3370 5048 5044 36436 34903 1681 1680
1681 1685 3351 3360 5019 5014 36109 36119 1683 1682
1681 1684 3349 3353 5035 5040 34113 35839 1681 1683
1688 1683 3352 3361 5023 5038 34358 34465 1694 1688
1691 1693 3344 3366 5007 4941 36528 36261 1682 1690
1681 1683 3359 3350 5003 4985 34711 35506 1681 1681
1681 1683 3372 3366 5050 5008 34661 35354 1681 1681
1680 1687 3353 3369 5008 4993 36280 34624 1688 1683
1681 1683 3357 3357 5013 5016 34308 35113 1680 1681
1688 1684 3361 3376 4997 4961 36415 34690 1682 1681
1683 1683 3306 3360 4918 4883 34441 35995 1682 1687
1684 1683 3342 3316 4933 4977 36079 34865 1685 1687
1693 1696 3359 3381 5004 4866 34812 35736 1682 1684
1682 1681 2022 1923 1980 2112 36356 34793 1680 1681
1682 1683 1792 1780 2250 1937 34239 35321 1682 1680
1683 1682 1850 1756 1980 1813 34531 34582 1685 1683
1689 1682 1817 1719 2006 1871 34612 35949 1680 1680
1680 1688 1734 1712 2013 1930 34915 36106 1681 1681
1684 1685 1784 1737 1833 1912 34105 37330 1687 1688
1686 1689 1763 1798 1850 1833 35540 34423 1686 1688
1686 1683 1749 1739 1809 1822 35474 35242 1680 1682
Avg operation time 1682.3 1685 3295 3295 4911 4903 36539 36711 1685 1685


Final Thought

Java virtual threads simplify development by eliminating the need for complex non-blocking reactive programming or manual tuning of thread pool sizes. Since applications can be written in a straightforward, synchronous style, developers can easily trace execution using standard stack traces in log files, greatly improving observability and debugging. 

Virtual threads also offer improved throughput and faster response times for concurrent, CPU-bound operations, thanks to reduced context-switching overhead and efficient scheduling managed by the JVM.

Java virtual machine Thread pool Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Virtual Threads: A Game-Changer for Concurrency
  • Java Thread Dump Analysis
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • The Energy Efficiency of JVMs and the Role of GraalVM

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: