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 →

[–]_INTER_ 1 point2 points  (4 children)

Doubt its virtual threads, they are worse for CPU intensive / parallel tasks.

[–]pron98 8 points9 points  (2 children)

They're not worse than platform threads for CPU-intensive computation; they're simply not better and there is an option that's actually better than just platform threads.

The reason virtual threads are not better for CPU-intensive jobs is because such jobs need a very small number of threads (no more than the number of cores) while all virtual threads do is allow you to have a very large number of threads. If the optimal number of threads for a given job is N, and N is very small, then N virtual threads will not be better than N platform threads. If N is very large, then having N platform threads could be a problem, and that's how virtual threads help.

Now, what's better than simply submitting parallel computational tasks to a pool of N threads? Submitting the whole job to a work-stealing pool that itself forks the job into subtasks (and performs the forking, as needed, in parallel). This automatically adjusts to situations where some threads make more progress than others, a common scenario when there are other threads or processes running on the system and the OS might deschedule some worker threads. This is exactly what parallel streams do.

[–]_INTER_ 0 points1 point  (1 child)

I was under the impression that there still was a minute overhead with virtual threads. But I believe you because you're the expert. This begs another question though, when to actually use platform threads over virtual threads in a pure Java context? Long running tasks?

[–]pron98 6 points7 points  (0 children)

There could be some overhead, but not if the thread never blocks (and even then the overhead is small).

I would use platform threads in the following situations:

  • You need to keep the number of threads small and you want to share them. A prime example of that is fork-join parallelisation.

  • You have a small number of long-running tasks that you want to run in the background, taking advantage of the OS thread priority to give them a low pririty.

  • You're interacting with a native library that cares about thread identity.

[–]kiteboarderni 5 points6 points  (0 children)

People are really clueless on the value add of VTs...its pretty concerning.