easiest way to force a write membar in jdk7 and 8

Vitaly Davidovich vitalyd at gmail.com
Sun Feb 22 16:03:03 UTC 2015


What version of java is this for? How are you publishing to other threads
exactly?

By the way, you should keep in that you'll most likely get false sharing in
the scheme you described.  Seeing "ar" in another thread is fine so long as
it doesn't try to read beyond the available range.

sent from my phone
On Feb 22, 2015 10:53 AM, "Andy Nuss" <andrew_nuss at yahoo.com> wrote:

> Hi,
>
> I tried posting a related question on the concurrency-interest mailing
> list but for some reason my messages are bouncing there but not here.
>
> Basically, the idea is that some thread has been working with an int[] and
> filling it with values.  From some range such as [0,8) of the array, that
> thread is sure never again to change those values.  Now it wishes to
> publish the int[] range in a threadsafe way in some new wrapper object.
>
> It cannot construct simply this (I believe), especially if somehow another
> thread has seen the "ar":
>
> class IntArray {
>     final int[] ar;
>     final int from;
>     final int to;
>     IntArray (int[] ar, int from, int to) {
>         this.ar = ar; this.from = from; this.to = to;
>     }
> }
>
> Would it work to use this:
>
> class IntArrayVolatile {
>     volatile int[] ar;
>     final int from;
>     final int to;
>     IntArrayVolatile (int[] ar, int from, int to) {
>         this.ar = ar; this.from = from; this.to = to;
>     }
>     IntArray publish () {
>         return new IntArray(ar, from, to);
>     }
> }
>
> and then in the thread do this:
>    int[] ar = new int[100];
>    ... fill values from [0, 8) never again to change
>    IntArray publishar = new IntArrayVolatile(ar, from, to).publish();
>    ... give publishar to other threads
>    ... keep filling values from position 8 and higher in "ar" and
> publishing further ranges in a similar way
>
> Obviously, this is alot about the internal workings of hotspot with the
> membars for volatile variables.
>
> Andy
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20150222/36d238b9/attachment.html>


More information about the hotspot-compiler-dev mailing list