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

Andrew Haley aph at redhat.com
Fri Jul 7 09:26:06 EDT 2017

On 07/07/17 13:27, Alex Otenko wrote:

> If you are updating a reference, then CAS can also work.

Well, that is the subject of some contention here, because the spec
*explicitly* says you should not compare quality, and that is what a
CAS does.  But the re is no need for a CAS; synchronized will do, and
it allows you to call equals() rather than using reference equality.

In practice, avoiding the use of reference equality is not a problem.
That is my point.

> If you are talking about imitating update of the reference by
> mutating inlined object contents,

You're not in this case.

> then you do need synchronized for readers.

Andrew Haley
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671

More information about the Concurrency-interest mailing list