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

Michael Hixson michael.hixson at gmail.com
Thu Jul 6 10:35:26 EDT 2017

On Thu, Jul 6, 2017 at 7:27 AM, Andrew Haley <aph at redhat.com> wrote:
> On 06/07/17 15:00, Michael Hixson wrote:
>> Am I understanding you correctly that the reason compareAndSet does
>> the wrong/unwanted thing here is not actually because of the ==
>> comparison and ties to object identity, but because the value can't be
>> written atomically?
> I was trying to clarify, but my explanation seems to have made things worse.
> No, I'm not saying that.  The value can't be written atomically, that
> is true.  I am saying that it would be nice if we could CAS the
> *value* of a java.time.Instant, but we can't.  All we can CAS is a
> reference to an Instant, but we're not supposed to do that because
> we're not supposed to compare value-based classes with reference
> equality.  So, it's best to use a synchronized block.
> --
> Andrew Haley
> Java Platform Lead Engineer
> Red Hat UK Ltd. <https://www.redhat.com>
> EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671

(re CC'ing the mailing list, oops)

Ah ok.  That makes sense, thanks.


More information about the Concurrency-interest mailing list