While working on AtomicInteger, I found this API provides two Methods.
compareAndExchange:
Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by
VarHandle.compareAndExchange(java.lang.Object...)
compareAndSet:
Atomically sets the value to
newValue
if the currentvalue == expectedValue
, with memory effects as specified byVarHandle.compareAndSet(java.lang.Object...)
.
I am unable to understand difference between two, Please help with suitable example.