News for hackers. Stuff that matters.
Re:Java Virtual Threads Ate My Memory: A Web Crawler's Tale ... by never_inline on Saturday May 31, 2025 @01:51AM (#44142124)
Stupid question: Why not provide a threadpool-like construct which may not necessarily keep threads around but limits their number?
Re:Java Virtual Threads Ate My Memory: A Web Crawler's Tale ... by jeroenhd on Saturday May 31, 2025 @03:27AM (#44142598)
The amount of actual threads is limited, by default to the amount of CPU cores the system has. The problem isn't the amount of threads itself, but the fact that the threadpool will keep taking on more work, and that it's efficient enough to blow past the CPU+I/O bottleneck. You can achieve the same problem with a threadpool if your threadpool is efficient enough.

Standard concurrency tools like semaphores are capable of reducing the amount of work being done at the same time. You could also simulate classic thread pools by using concurrent lists/sets/etc to replicate traditional work queues, but you'd probably lose any advantage you'd gained from switching to green threads in the first place.

Re:Java Virtual Threads Ate My Memory: A Web Crawler's Tale ... by 708145_ on Saturday May 31, 2025 @06:15AM (#44143240)
Of course it possible to limit the number of virtual threads. A web server can have a limit on number of virtual threads too, and queue incoming request before dispatching to to workers (virtual threads).

As other have said, this can be achieved with a semaphore and the bulkhead pattern. You can also limit the number of number of connections.

I would expect any "production ready" web server using virtual threads having some sort of limiting. That must be the case right?

Re:Java Virtual Threads Ate My Memory: A Web Crawler's Tale ... by pron on Saturday May 31, 2025 @06:55AM (#44143408)
What you usually want to limit isn't the number of threads but the number of threads doing a particular operation, such as accessing some service, so it's more flexible (and efficient) to just guard that particular operation with a semaphore.

The only place where you may want to limit the number of threads is at the entry to the system, but since the semaphore is the construct for limiting the number of anything, you might as well just use that there, too. For example, if the number of requests currently being processed is too high (above the semaphore's number of leases), you don't accept new connections.

Re:Java Virtual Threads Ate My Memory: A Web Crawler's Tale ... by andersmurphy on Saturday May 31, 2025 @09:52AM (#44144285)
Good, question. The simple answer is there already is such a construct on the JVM already: the semaphore. People just forget it exists. I wrote an article about it a while back[1].

1. Managing throughput with virtual threads (it's in Clojure, but it's using the underlying Java APIs)

https://andersmurphy.com/2024/05/06/clojure-managing-through...