5

I often find myself using this idiom:

AtomicReference<MyCoolObject> coolReference = new AtomicReference(new MyCoolObject());

getOptionalValue().ifPresent(presentValue -> {
  coolReference.set(new MyCoolObject(presentValue));
});
return coolReference.get(); 

Is this a bad habit, or a legitimate use of AtomicReference?

shmosel
  • 49,289
  • 6
  • 73
  • 138
afspear
  • 51
  • 1
  • 3

0 Answers0