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
  • 45,768
  • 6
  • 62
  • 130
afspear
  • 51
  • 3

0 Answers0