I have a bit of a unique situation. I'd like to be able to use the compareAndSet() feature of AtomicReference but with a bit of a twist.
Normally you use compareAndSet() to compare against a single, expected value before you actually change it. In my case the expected value to compare against could be one of many values.
Is there a way to do this using AtomicReference? If not, what other techniques should I use? Perhaps just traditional use of synchronized?
I'm going to assume that you have a
Setof expected values. In that case...This is typical for how uses of
compareAndSetwork: they run in a loop until theAtomicReferencedoesn't change between thegetand thecompareAndSet.