[concurrency-interest] Difference between AtomicReference.getPlain() and getOpaque()

Andrew Haley aph at redhat.com
Sun Oct 15 09:28:29 EDT 2017


On 15/10/17 10:23, Florian Weimer wrote:
> Thanks, with this and (the reference Никита provided), it's clear that
> they are not the same.  It's really not obvious based on JLS 9 and the
> JDK 9 API documentation that there are additional constraints on the
> implementation here.  The description of the memory model in the JLS
> could at least say that it's not been updated for the introduction of
> opaque accesses in JDK 9.

"Obvious" is perhaps not the word to use in this context.  It seems to
be extraordinarily difficult to specify extensions to the JMM (and,
indeed, the JMM itself) in a way that makes sense to most Java
programmers.  But the behaviour I described is implied by the spec:

getPlain()

     * Returns the current value, with memory semantics of reading as
     * if the variable was declared non-{@code volatile}.

getOpaque()

     * Returns the value of a variable, accessed in program order, but
     * with no assurance of memory ordering effects with respect to
     * other threads.

The key phrase here is "accessed in program order," but most
programmers won't notice its significance.

-- 
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