In java 17, AtomicReference
has the compareAndExchange
method which is like compareAndSet
, but instead of returning a boolean, it returns the value right before the atomic action. I need that to implement a custom concurrent structure.
Due to limitations on the project, I have to use only Java 8 features. Some digging revealed VarHandle
which has compareAndExchange
. However, VarHandle
requires Java 9.
Therefore, it seems that I have to implement the compareAndExchange
myself. But how to do so efficiently with the existing methods? (And what about the compareAndExchangeWeak
version?)
(I cannot rely on any third-party libraries, BTW)