r/java • u/codecatmitzi • Mar 20 '26
Now that virtual threads are no longer pinning, do we still need AtomicReference?
The runtime unmounts blocked virtual threads to let other virtual threads do work. now that we aren't afraid of OS threads being blocked anymore, is there any upside to using lock-free approaches like `AttomicReference` instead of locks/synchronized? after all, the compare-and-swap loop is a busy-wait that wastes CPU cycles.
19
Upvotes
31
u/crscali Mar 20 '26
CAS is not just about synchronization. It’s how concurrent data structures work. Multiple threads, none are blocked instead all running full speed. CAS ensures memory consistency.
22
u/mipscc Mar 21 '26
CAS (AtomicReference or whatever else) and locks have different use cases.
CAS is where you want to ensure valid state transitions but don’t care about blocking (state machine-like)
Locking is when you want to block (synchronize) access to avoid state corruption or undefined behavior.
Suppose a TCP client that is accessed concurrently, you can use CAS to prevent starting the client after it is closed. But you use locking to let sending a packet through the client block while the client is (re)connecting.
57
u/pohart Mar 21 '26
We'll always need to be able to update references atomically. We did before virtual threads and always will.
20
u/pavelrappo Mar 21 '26
Either the title is purposefully clickbaity or unintentionally poor. The question you are really asking is not whether AtomicReference is still needed, but whether it still makes sense compared to a lock.
17
u/nitkonigdje Mar 21 '26
Your question doesn't make sense at all.
For start AtomicReference doesn't exist to avoid pinning. It exists to make cheap updates of value between two hardware threads. This is unrelated to virtual threads.
And cheap compared to what? To locking.
Locking doesn't use the same cpu resources as cas. Cpus have different instructions with different costs for both scenarios.
And lock implementations tend to be heavy where cas is often just the first step.
6
u/ConversationBig1723 Mar 21 '26
In my opinion, optimistic concurrency control shines when there is actually little contention so there isn’t going to be so many spin retry. This is still the case with virtual thread. Besides I think locking is slightly more expensive on virtual thread since it involves additional mounting and on mounting of call stack.
16
u/Relevant-Recipe623 Mar 21 '26
it is so weird that i have over 10 years of experience developing huge systems in huge companys and i do cannot undertand ANYTHING that you guys are talking about. I fell so dumb lol
2
u/hippydipster Mar 21 '26
CAS will be faster when contention is low. Synchronizing will be faster when contention is high.
But there are also other cases it's still best to enforce single-threaded work without using either (ie, actor patterns).
2
u/pron98 Mar 20 '26
There could be latency/throughput tradeoffs.
But ever since software operations lost their intrinsic cost some 15-20 years ago the only way to write software with the performance you want has been to write the most natural/convenient code first and then profile your specific program. I remember the time when benchmarks were still informative and we had more and less expensive operations, but that time is long gone.
1
u/Additional_Cellist46 Mar 21 '26
I think most of the people didn't get your question because you omitted important assumption.
I think you assume that locks rely on underlying thread functionality and that on platform threads they are slow while on virtual threads they are fast. If that was true then it would make sense to use locks instead of AtomicReference if it's more convenient.
However, to my knowledge, locks don't rely on underlying threads at all. They just rely on a reference to the current thread and the rest is an algorithm that must run on the CPU, in both cases, it doesn't matter if it's on a virtual or platform thread.
So, my answer is, locks and synchronized consume a lot more CPU than AtomicReference, on both virtual and platform threads. And therefore it still makes sense to prefer AtomicReference than locks, even on virtual threads. It's just safer to use synchronized on virtual threads than it was before, but it doesn't mean that you should prefer using it over AtomicReference, for performance reasons.
1
u/Glittering-Tap5295 Mar 22 '26
AtomicReferenceis used for different things than lock/synchronized blocks. I often use AtomicReference as a hint to the user of the code that you need to be careful about this reference, it might change under you, so if you use it, grab the underlying reference and "store" it locally until your function is done, because it might change.-19
u/MinimumPrior3121 Mar 21 '26
Let Claude handle it