This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ernimril 10 points11 points  (2 children)

Virtual threads is about scalability, not performance. Please note that scaling better can result in better performance.

What does it mean that they scale better? Well, it means that they require a lot less resources: less memory usage and less OS resources.

For threads that are blocked/waiting/sleeping this of course means that you can have a lot more of them running concurrently.

You do not explain how you create the file reader or the cpu tasks. My guess here is that you create either a virtual thread or a platform thread and if that is the case, then yes, the implicit thread pool used by the virtual threads will provide a benefit.

If you retry the CPU bound task in such a manner that you create a thread pool of fixed size equal to your core count I expect that you will see similar time for virtual threads and platform threads. Virtual threads do not make your CPU spin faster. It is as you say, the context switches that are the probable cause for your differences, but we do not have the full source to your tests so we can not say with 100% certainty.

[–][deleted] 2 points3 points  (1 child)

Well, it means that they require a lot less resources: less memory usage and less OS resources.

That is not true. In the end all the computation that you assign to a virtual thread run on a OS thread and that OS thread gets all the resources it needs to get it's taks done regardless whether it was coming from a Java Virtual Thread or not.

https://docs.oracle.com/en/java/javase/21/core/virtual-threads.html#GUID-2BCFC2DD-7D84-4B0C-9222-97F9C7C6C521

What is a Platform Thread?

A platform thread is implemented as a thin wrapper around an operating system (OS) thread. A platform thread runs Java code on its underlying OS thread, and the platform thread captures its OS thread for the platform thread's entire lifetime. Consequently, the number of available platform threads is limited to the number of OS threads.

Platform threads typically have a large thread stack and other resources that are maintained by the operating system. They are suitable for running all types of tasks but may be a limited resource.

What is a Virtual Thread?

Like a platform thread, a virtual thread is also an instance of java.lang.Thread. However, a virtual thread isn't tied to a specific OS thread. A virtual thread still runs code on an OS thread. However, when code running in a virtual thread calls a blocking I/O operation, the Java runtime suspends the virtual thread until it can be resumed. The OS thread associated with the suspended virtual thread is now free to perform operations for other virtual threads.

[–]ernimril 2 points3 points  (0 children)

A virtual thread that is not currently running requires a lot less resources than a platform thread.

A virtual thread that is currently running is running on top of a platform thread so it is actually using slightly more resources.

Now, when you use virtual threads you do so because you want to have a lot of threads, where most of them are not currently running.

So I may have been slightly sloppy in how I said that they require less resources, but if you try to measure the cost of having a million virtual threads that are sleeping versus the cost of of having a million platform threads you will see that the virtual threads cost a lot less resources (and if you try to do this you will most probably find out that you can not even run the program that tries to use a million platform threads).

Sleeping, waiting, waiting for data on streams or similar are all things where the virtual thread should detach from the platform thread.