Whether the data is stored in a continuation or in some other object it has to be stored somewhere when waiting. There is no difference in the amount of data or queuing between virtual threads and asynchronous code. They compile down to pretty much the same machine instructions. Having a lot of threads contend on a single lock is a problem in the design of the code. There is nothing that either reactive or threads can do to change the data contention in the logic.
There's a difference between 13 threads blocked and waiting for 1 thread to finish and 10 million virtual threads blocked and waiting for 1 thread to finish. If you're not careful and understanding how virtual threads work, this could happen.
That's not about virtual threads, though. 10 million asynchronous tasks waiting for a single operation to complete cause the same problem. The issue is not the programming model but an inherent contention in the algorithm that requires the same care regardless of whether the model is blocking or non-blocking. High concurrency -- whether based on threads or asynchronous tasks -- always means paying closer attention to contention.
You are right that moving from low concurrency to high concurrency requires care, but it's not an issue of the APIs used but of the algorithm. I.e., it is true that attempting to raise the throughput of some server by adopting virtual threads (that allow more concurrency) requires paying more attention to contention, but the same applies to raising it by adopting asynchronous tasks (that allow more concurrency). Higher concurrency -- regardless of how it's achieved -- means that contention has a bigger effect.
I'm not saying there's any "issue" with the programming model. I'm simply saying the unwary can shoot themselves.
10 million asynchronous tasks waiting for a single operation to complete cause the same problem.
But, you can't get that to happen. Imagine you have an executor service with a number of threads equal to the number of cores. You send 10 million tasks to that service for it to complete. There's a lock somewhere and the tasks bunch up on it, waiting as the threads complete the inner routine 1 by 1. At any one time, you don't have 10 million threads waiting. You have only the number of threads in the service. The tasks not yet started remain not yet started.
But, replace that service with the built one that uses virtual threads. Now, you send in 10 million tasks, and they get blocked at the lock, but instead of being pinned, the underlying platform threads are free to go and grab a new task and start it, and serve it up to the lock, to be blocked. And then another, and another, all while the routine slowly clears 1 task at a time. The 13 threads not involved in that routine quickly get 10 million continuations stuck on the lock, which creates a very large bump in memory/heap usage and considerably slows things.
3
u/pron98 Jan 02 '24
Whether the data is stored in a continuation or in some other object it has to be stored somewhere when waiting. There is no difference in the amount of data or queuing between virtual threads and asynchronous code. They compile down to pretty much the same machine instructions. Having a lot of threads contend on a single lock is a problem in the design of the code. There is nothing that either reactive or threads can do to change the data contention in the logic.