How Java Virtual Threads Work?

Virtual Threads are lightweight threads introduced in Java 19 (Preview) and Java 21 (Stable). They allow Java to create millions of threads efficiently, helping handle concurrent tasks without wasting memory or CPU.

Virtual Threads do not map 1:1 to OS Threads and do not replace the original Platform Threads. The Platform Threads are backed by the OS Threads and are sometimes also known as Carrier Threads in this context.

Think of the Platform Threads as a small group of workers, and Virtual threads as tasks. With Virtual threads, tasks are given to workers only when needed, allowing one worker to handle thousands of tasks efficiently.

Here’s how Virtual Threads work:

Virtual Threads run on top of Platform Threads. The JVM schedules them onto a small number of Platform Threads.

When a Virtual Thread starts, the JVM assigns it to a normal OS-backed Platform Thread.

Virtual Threads can also handle CPU-intensive work, but their real advantage is in scenarios with a high number of I/O-bound or concurrent tasks.

If the Virtual Thread performs a blocking operation (like I/O, database call, sleep, etc.), the JVM unmounts it from the Platform Thread. However, this does not block the underlying OS Thread as such.

The Platform thread is freed up to handle another Virtual Thread.

When the blocking operation finishes, the Virtual Thread is rescheduled on any available Platform thread

technology #innovation #future #techupdate” #technews #futuretech #innovation #AI #automation #techtrends #digitaltransformation #fullstackdeveloper

reactdeveloper

原文链接:How Java Virtual Threads Work?

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
It is during our darkest moments that we must focus to see the light.
越是在艰难困苦的时候,我们越是要看到希望
评论 抢沙发

请登录后发表评论

    暂无评论内容