[concurrency-interest] Should I avoid compareAndSet with value-based classes?
aph at redhat.com
Fri Jul 7 14:16:30 EDT 2017
On 07/07/17 18:41, Alex Otenko wrote:
> The value class instance does not necessarily fit in a single
> machine word - time Instance certainly doesn’t. If the time Instance
> is “inlined” by the JVM, then you can’t guarantee atomicity of
> writes, and atomicity of reads also, so the reads also need to use a
> synchronized block - whether demand that explicitly from the
> programmer, or do that implicitly, like it’s done for volatile long
> on some platforms, does not matter. (Or use StampedLock, as Doug
> points out).
To be able to see a partly-constructed or overlapped instance of a
value-based class would be a clear violation of the JLS, so we don't
have to worry about that. Any VM designer who wants to embed
instances of value-based classes is going to have to figure out how to
guarantee atomicity of updates.
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