[concurrency-interest] Fwd: Should I avoid compareAndSet with value-based classes?

Justin Sampson jsampson at guidewire.com
Fri Jul 7 15:07:55 EDT 2017


Gil Tene wrote:

> public void setInstant(final Instant newInstant) {
>   instantHolder.get().setInstant(newInstant);
> }

This is broken because there's no volatile write. It needs to be instantHolder.set(new BoxedInstant(newInstant)), and then BoxedInstant can be immutable. I don't think you can get away from instantiating a new box each time, if your interpretation is correct.

Cheers,
Justin



More information about the Concurrency-interest mailing list