easiest way to force a write membar in jdk7 and 8
Andy Nuss
andrew_nuss at yahoo.com
Sun Feb 22 15:52:41 UTC 2015
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/716bd1e9/attachment-0001.html>
More information about the hotspot-compiler-dev
mailing list