{"id":210,"date":"2024-03-22T21:40:00","date_gmt":"2024-03-22T13:40:00","guid":{"rendered":"https:\/\/blog.bexonbai.com\/?p=210"},"modified":"2026-03-02T21:43:09","modified_gmt":"2026-03-02T13:43:09","slug":"virtual-threads","status":"publish","type":"post","link":"https:\/\/blog.bexonbai.com\/?p=210","title":{"rendered":"Virtual Threads"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">As an Android developer, now I use Kotlin all the time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Java 19, Java brought the feature of virtual threading, which is all too familiar to Kotlin, a Java version of Coroutines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you know, OS switching threads is an expensive affair. Java\u2019s Thread is a wrapper implementation for OS Thread, so there are as many Java threads as there are OS threads.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Like this<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1724\" height=\"1155\" src=\"https:\/\/blog.bexonbai.com\/wp-content\/uploads\/2026\/03\/1000002220.jpg\" alt=\"1000002220.jpg\" class=\"wp-image-211\" srcset=\"https:\/\/blog.bexonbai.com\/wp-content\/uploads\/2026\/03\/1000002220.jpg 1724w, https:\/\/blog.bexonbai.com\/wp-content\/uploads\/2026\/03\/1000002220-300x201.jpg 300w, https:\/\/blog.bexonbai.com\/wp-content\/uploads\/2026\/03\/1000002220-1024x686.jpg 1024w, https:\/\/blog.bexonbai.com\/wp-content\/uploads\/2026\/03\/1000002220-768x515.jpg 768w, https:\/\/blog.bexonbai.com\/wp-content\/uploads\/2026\/03\/1000002220-1536x1029.jpg 1536w\" sizes=\"auto, (max-width: 1724px) 100vw, 1724px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">But most of the time we my operations are lightweight, but using an entire thread is very wasteful.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One of the most important concepts of virtual threads is<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Non-blocking hangs<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Co-threading(Virtual Thread) is cutting threads<\/li>\n\n\n\n<li>A hang is a tangent thread that can automatically cut back<\/li>\n\n\n\n<li>Hanging is non-blocking in the sense that it can write non-blocking operations in code that looks blocking<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">So Virtual Thread are&nbsp;<strong>not<\/strong>&nbsp;the real Thread, it a&nbsp;<strong>Carrier Thread.<\/strong>&nbsp;It\u2019s still run in the Thread, and if you have only one business it has the same overhead as regular Thread.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1673\" height=\"1526\" src=\"https:\/\/blog.bexonbai.com\/wp-content\/uploads\/2026\/03\/1000002221.jpg\" alt=\"1000002221.jpg\" class=\"wp-image-212\" srcset=\"https:\/\/blog.bexonbai.com\/wp-content\/uploads\/2026\/03\/1000002221.jpg 1673w, https:\/\/blog.bexonbai.com\/wp-content\/uploads\/2026\/03\/1000002221-300x274.jpg 300w, https:\/\/blog.bexonbai.com\/wp-content\/uploads\/2026\/03\/1000002221-1024x934.jpg 1024w, https:\/\/blog.bexonbai.com\/wp-content\/uploads\/2026\/03\/1000002221-768x701.jpg 768w, https:\/\/blog.bexonbai.com\/wp-content\/uploads\/2026\/03\/1000002221-1536x1401.jpg 1536w\" sizes=\"auto, (max-width: 1673px) 100vw, 1673px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Java\u2019s Virtual Threads are mainly these two functions<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Thread<\/strong><strong>.<\/strong>ofVirtual<strong>()<\/strong>\n<strong>Executors<\/strong><strong>.<\/strong>newVirtualThreadPerTaskExecutor<strong>()<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s an example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>private<\/strong> <strong>static<\/strong> <strong>void<\/strong> <strong>runOnThread<\/strong><strong>()<\/strong> <strong>{<\/strong>\n    <strong>final<\/strong> <strong>AtomicInteger<\/strong> atomicInteger <strong>=<\/strong> <strong>new<\/strong> <strong>AtomicInteger<\/strong><strong>();<\/strong>\n    <strong>Runnable<\/strong> runnable <strong>=<\/strong> <strong>()<\/strong> <strong>-><\/strong> <strong>{<\/strong>\n        <strong>try<\/strong> <strong>{<\/strong>\n            <strong>Thread<\/strong><strong>.<\/strong>sleep<strong>(<\/strong><strong>Duration<\/strong><strong>.<\/strong>ofMillis<strong>(<\/strong>500<strong>));<\/strong>\n        <strong>}<\/strong> <strong>catch<\/strong> <strong>(<\/strong><strong>InterruptedException<\/strong> e<strong>)<\/strong> <strong>{<\/strong>\n            <strong>throw<\/strong> <strong>new<\/strong> <strong>RuntimeException<\/strong><strong>(<\/strong>e<strong>);<\/strong>\n        <strong>}<\/strong>\n        <strong>System<\/strong><strong>.<\/strong>out<strong>.<\/strong>println<strong>(<\/strong>\"Work Done - \" <strong>+<\/strong> atomicInteger<strong>.<\/strong>incrementAndGet<strong>());<\/strong>\n    <strong>};<\/strong>\n    <strong>Instant<\/strong> start <strong>=<\/strong> <strong>Instant<\/strong><strong>.<\/strong>now<strong>();<\/strong>\n    <strong>try<\/strong> <strong>(<\/strong><strong>var<\/strong> executor <strong>=<\/strong> <strong>Executors<\/strong><strong>.<\/strong>newFixedThreadPool<strong>(<\/strong>100<strong>))<\/strong> <strong>{<\/strong>\n        <strong>for<\/strong><strong>(<\/strong><strong>int<\/strong> i <strong>=<\/strong> 0<strong>;<\/strong> i <strong>&lt;<\/strong> 500<strong>;<\/strong> i<strong>++)<\/strong> <strong>{<\/strong>\n            executor<strong>.<\/strong>submit<strong>(<\/strong>runnable<strong>);<\/strong>\n        <strong>}<\/strong>\n    <strong>}<\/strong>\n\n    <strong>Instant<\/strong> finish <strong>=<\/strong> <strong>Instant<\/strong><strong>.<\/strong>now<strong>();<\/strong>\n    <strong>long<\/strong> timeElapsed <strong>=<\/strong> <strong>Duration<\/strong><strong>.<\/strong>between<strong>(<\/strong>start<strong>,<\/strong> finish<strong>).<\/strong>toMillis<strong>();<\/strong>\n    <strong>System<\/strong><strong>.<\/strong>out<strong>.<\/strong>println<strong>(<\/strong>\"Thread total time: \" <strong>+<\/strong> timeElapsed <strong>+<\/strong> \"ms\"<strong>);<\/strong>\n<strong>}<\/strong>\n\n<strong>private<\/strong> <strong>static<\/strong> <strong>void<\/strong> <strong>runOnVirtualThread<\/strong><strong>()<\/strong> <strong>throws<\/strong> <strong>InterruptedException<\/strong> <strong>{<\/strong>\n    <strong>final<\/strong> <strong>AtomicInteger<\/strong> atomicInteger <strong>=<\/strong> <strong>new<\/strong> <strong>AtomicInteger<\/strong><strong>();<\/strong>\n    <strong>Runnable<\/strong> runnable <strong>=<\/strong> <strong>()<\/strong> <strong>-><\/strong> <strong>{<\/strong>\n        <strong>try<\/strong> <strong>{<\/strong>\n            <strong>Thread<\/strong><strong>.<\/strong>sleep<strong>(<\/strong><strong>Duration<\/strong><strong>.<\/strong>ofMillis<strong>(<\/strong>500<strong>));<\/strong>\n        <strong>}<\/strong> <strong>catch<\/strong> <strong>(<\/strong><strong>InterruptedException<\/strong> e<strong>)<\/strong> <strong>{<\/strong>\n            <strong>throw<\/strong> <strong>new<\/strong> <strong>RuntimeException<\/strong><strong>(<\/strong>e<strong>);<\/strong>\n        <strong>}<\/strong>\n        <strong>System<\/strong><strong>.<\/strong>out<strong>.<\/strong>println<strong>(<\/strong>\"Work Done - \" <strong>+<\/strong> atomicInteger<strong>.<\/strong>incrementAndGet<strong>());<\/strong>\n    <strong>};<\/strong>\n    <strong>Instant<\/strong> start <strong>=<\/strong> <strong>Instant<\/strong><strong>.<\/strong>now<strong>();<\/strong>\n    <strong>try<\/strong> <strong>(<\/strong><strong>var<\/strong> executor <strong>=<\/strong> <strong>Executors<\/strong><strong>.<\/strong>newVirtualThreadPerTaskExecutor<strong>())<\/strong> <strong>{<\/strong>\n        <strong>for<\/strong><strong>(<\/strong><strong>int<\/strong> i <strong>=<\/strong> 0<strong>;<\/strong> i <strong>&lt;<\/strong> 500<strong>;<\/strong> i<strong>++)<\/strong> <strong>{<\/strong>\n            executor<strong>.<\/strong>submit<strong>(<\/strong>runnable<strong>);<\/strong>\n        <strong>}<\/strong>\n    <strong>}<\/strong>\n\n    <strong>Instant<\/strong> finish <strong>=<\/strong> <strong>Instant<\/strong><strong>.<\/strong>now<strong>();<\/strong>\n    <strong>long<\/strong> timeElapsed <strong>=<\/strong> <strong>Duration<\/strong><strong>.<\/strong>between<strong>(<\/strong>start<strong>,<\/strong> finish<strong>).<\/strong>toMillis<strong>();<\/strong>\n    <strong>System<\/strong><strong>.<\/strong>out<strong>.<\/strong>println<strong>(<\/strong>\"Virtual Thread total time: \" <strong>+<\/strong> timeElapsed <strong>+<\/strong> \"ms\"<strong>);<\/strong>\n<strong>}<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We perform the same task using Threads and Virtual Threads respectively.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use&nbsp;<code>Thread.sleep(Duration.ofMillis(500));<\/code>&nbsp;to simulate time-consuming IO blocking tasks.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Thread<\/strong> total time: 2530ms\n<strong>Virtual<\/strong> <strong>Thread<\/strong> total time: 510ms<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The task in this example is simple code-sleep for half second-modern hardware can easily support 500 virtual threads running this kind of code simultaneously.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JDK runs the code on a small number of OS threads, perhaps just one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But in&nbsp;<code>runOnThread()<\/code>, JDK really created 500 threads.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s a huge expense. \ud83d\udcb5<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Virtual Threads were introduced in Java 19 and finalized in Java 21 as part of the Loom project.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-210","post","type-post","status-publish","format-standard","hentry","category-develop"],"_links":{"self":[{"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/posts\/210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=210"}],"version-history":[{"count":1,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":213,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions\/213"}],"wp:attachment":[{"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}