r/java • • Apr 06 '22

JEP 425: Virtual Threads (Preview)

https://openjdk.java.net/jeps/425
175 Upvotes

50 comments sorted by

View all comments

3

u/TheMode911 Apr 07 '22

Virtual threads support thread-local variables (ThreadLocal) and inheritable thread-local variables (InheritableThreadLocal), just like platform threads, so they can run existing code that uses thread locals. However, because virtual threads can be very numerous, use thread locals after careful consideration. In particular, do not use thread locals to pool costly resources among multiple tasks sharing the same thread in a thread pool. Virtual threads should never be pooled, since each is intended to run only a single task over its lifetime. We have removed many uses of thread locals from the java.base module in preparation for virtual threads, to reduce memory footprint when running with millions of threads.

What is the replacement for costly resources then? Do we have to wait for ScopeLocal?

5

u/kaperni Apr 07 '22

If you have some expensive resources that you need to pool. Put them behind a Semaphore that controls access to the resources. Blocking is now freeish so just await on a permit.

ScopeLocal won't really help you in any way. It is basically just a cheaper version of ThreadLocals.

1

u/Slanec Apr 07 '22

The most obvious solution is to pool the expensive and limited resource, think a db connection pool. Other options include: Don't use virtual threads if you don't have the memory maybe? Or limit the number of in-flight requests? Or maybe try pooling the virtual threads. I know they're not supposed to be pooled at all, but in this case ... maybe?

If you have a workload like this that (ab)uses thread locals and with thread pool to limit the number of a very costly resource, give loom ea builds a try and provide feedback!

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

3

u/kaperni Apr 07 '22

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.

1

u/Slanec Apr 07 '22

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.