[concurrency-interest] Safe publishing strategy

Vitaly Davidovich vitalyd at gmail.com
Fri Jan 23 17:42:36 EST 2015


Yes, I know compiler is allowed to do that (without final and volatile*),
but I was having a hard time imagining where it would actually make that
type of transformation because it doesn't seem profitable at first thought.

In the case you observed, did it actually make sense for it to have done
that? I.e. was there some tangible optimization or was it just some
artifact of implementation?

* - assuming we're all in agreement now that volatile has the same effect,
although not officially in current JMM

On Fri, Jan 23, 2015 at 5:37 PM, Aleksey Shipilev <
aleksey.shipilev at oracle.com> wrote:

> On 01/24/2015 12:25 AM, Vitaly Davidovich wrote:
> >     Yes, if you have enough field stores to spill over the publication of
> >     the object. See also:
> >      http://shipilev.net/blog/2014/safe-public-construction/
> >
> >
> > Can you elaborate on what you mean by "enough field stores to spill over
> > the publication"?
>
> If you do quite a few non-final field stores in constructor, the
> instruction scheduler may decide to emit a few field stores, then
> publish the reference, then emit the rest of the field stores. I managed
> to observe this at least once back in JDK 7 and 32-bit VMs days on a
> real-life application.
>
> Out of curiosity, I slapped together a reproducer with jcstress:
>
> http://hg.openjdk.java.net/code-tools/jcstress/file/7cd3dfada49b/tests-custom/src/main/java/org/openjdk/jcstress/tests/unsafe/UnsafePublication.java
>
> On my i7-4790K, JDK 8u40 EA, Linux x86_64, it does:
>
> $ java -XX:-UseCompressedOops -jar tests-custom/target/jcstress.jar -t
> .*UnsafePublication.*  -v
>
> [OK] o.o.j.t.unsafe.UnsafePublication
> Observed state     Occurrences    Expectation   Interpretation
>           [-1] (   97,403,388)     ACCEPTABLE   Not published yet.
>            [0] (        2,731)     ACCEPTABLE   0 fields visible.
>            [1] (        6,099)     ACCEPTABLE   1 field visible.
>            [2] (        6,533)     ACCEPTABLE   2 fields visible.
>            [3] (       17,607)     ACCEPTABLE   3 fields visible.
>            [4] (   24,692,082)     ACCEPTABLE   All fields visible.
>
> Tell me something about "theoretical" again?
>
> Thanks,
> -Aleksey.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20150123/e2fe8604/attachment.html>


More information about the Concurrency-interest mailing list