Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make virtual-thread-friendly #1018

Closed
alexandergunnarson opened this issue Jun 13, 2023 · 1 comment
Closed

Make virtual-thread-friendly #1018

alexandergunnarson opened this issue Jun 13, 2023 · 1 comment

Comments

@alexandergunnarson
Copy link

Virtual threads are available as a preview feature in JDK 19 and will come out of preview in JDK 21 (https://openjdk.org/jeps/444). It would be nice to be able to use them with Caffeine without incurring the performance penalty of thread pinning, which blocks all other virtual threads on the carrier thread during a synchronized block or native call. The proposal here is to follow the JEP and "... avoid ... pinning by revising synchronized blocks ... to use java.util.concurrent.locks.ReentrantLock instead."

@ben-manes
Copy link
Owner

AsyncCache is virtual thread friendly.

The JEP authors and users have a more serious issue that ConcurrentHashMap is build on top of synchronized monitors so computations will pin the thread. Caffeine is layered on top of that, so regardless of what we did a synchronous cache would suffer from this problem. This feature gap is fairly serious such that most applications should not use Virtual Threads in JDK 21, as it is stable but not yet a broadly a good fit. Last I spoke with Doug he was musing about removing monitor locks, but it was all TBD / not figured out / not a priority. I would expect the JDK team to move faster than he will as being dept chair takes up most of his time. If ConcurrentHashMap switched and the JDK team gave up on synchronized then we'd have something to do, but at the moment it is in their court and not ours. You can use AsyncCache in the narrow cases when virtual threads are applicable, or wait until they fix its limitations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants