Hacker News new | ask | show | jobs
by pron 768 days ago
Pooling virtual threads has no upside and potentially a bit of downside: 1. You hang on for unused objects for longer instead of returning them to the more general pool that is the GC; 2. You risk leaking context between multiple tasks sharing the thread which may have security implications. Because of these and similar downsides you should only ever pool objects that give you benefit when they're shared -- e.g. they're expensive to create -- and shouldn't pool objects otherwise.
2 comments

Thank you for this, but thank you especially for virtual threads! They are awesome!

Is point 2 a virtual-thread only risk, or would we incur it with regular threads too?

Thank you! You incur this risk when pooling any kind of thread, too, but with platform threads at least pooling makes sense because they're costly, so you just need to be careful with thread locals on a shared thread pool. Not needing to share threads and potentially leak context is a security advantage of virtual threads.
Thank you for the explanation and for all the hard work on bringing virtual threads to the JVM. It's a really awesome feature.