0

If a class has a private AtomicReference member, and it only calls AtomicReference#get and AtomicReference#set, then is it (a) safe and (b) useful to replace that with a nullable volatile member?

aaronstacy
  • 6,189
  • 13
  • 59
  • 72

1 Answers1

1

As answered here, if you will just be using the get() and set() methods, there is no real difference between the two.

Is it safe? Yes.

Is it useful? Debatable, considering what additional functionality you might need for the future. If you want to keep it as simple as possible, volatile might (arguably) offer slightly better readability. I generally stick to AtomicReference because I can't always predict future requirements and I don't much like changing variable types when it can be avoided.

Roney Michael
  • 3,964
  • 5
  • 30
  • 45