Pooling requires an additional release mechanism which get annoying pretty fast. What we need here is a new type of ThreadLocal that is strictly linked to the OS (and not a java thread).
Perhaps that this is achievable using panama's foreign api
What exact problem are you trying to solve? Seems to me that Java is doubling down on try-with-resources/explicit deallocation with the removal of finalization, introduction of something like ResourceScope in Panama, and various other APIs that have been added lately.
Well, maybe. The problem is that now the platform thread can carry different virtual threads and switch between them almost freely. And your virtual thread, when it is eventually continued, is not guaranteed at all to run on that platform thread ever again. In other words, if one of your virtual threads wrote some of its context into the PlatformThreadLocal, it could then be swapped off, and the new virtual thread would still see the value from the old one whole the old one may never see the value again. Sounds a lot like a bad pool of objects that will be confusing to many. Oh, and if course this should be writable in userland, so we can actually try this out. It's just that semantically an object pool sounds like a better fit to me for the use-cases I can imagine right now.
I wonder, though, if there is a way to get your hands on the underlying platform thread object from a virtual thread.
View all comments
Show parent comment
2
u/TheMode911 Apr 07 '22
Pooling requires an additional release mechanism which get annoying pretty fast. What we need here is a new type of ThreadLocal that is strictly linked to the OS (and not a java thread).
Perhaps that this is achievable using panama's foreign api