loop invariant code motion and the jit
Andrew Haley
aph at redhat.com
Thu Jan 19 09:08:11 UTC 2017
On 18/01/17 20:04, Peter Veentjer wrote:
> private boolean stop;
> private long value;
>
> @Override
> public void run() {
> while (!stop) {
> value++;
> }
> }
>
> In theory the read of stop can be pulled out of the loop since stop isn't
> properly synchronised (volatile would do the trick).
In practice, too.
> Is there any prevention of this optimisation being applied to protect users
> against improper synchronised code?
No. If you want this to work properly, use a Reference or (in JDK9) a
VarHandle or a Fence.
Andrew.
More information about the hotspot-compiler-dev
mailing list