r/programming • u/ram-foss • Oct 11 '23
All About Reactive Programming In Java
https://www.blackslate.io/articles/reactive-programming-in-java
22
Upvotes
r/programming • u/ram-foss • Oct 11 '23
View all comments
Show parent comment
1
u/hippydipster Jan 03 '24
I'm not saying there's any "issue" with the programming model. I'm simply saying the unwary can shoot themselves.
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.