[jmm-dev] easily observable examples of non-sc behaviours in Java

Hans Boehm boehm at acm.org
Thu Jan 15 19:07:27 UTC 2015

A similar, though somewhat simpler and more standard, example seems to be

while (!flag) {}

getting optimized to tmp = flag; while(!tmp) {}

I have no idea which JVMs currently do that, but it seems to me that they

On Thu, Jan 15, 2015 at 9:31 AM, Francesco Zappa Nardelli <
francesco.zappa_nardelli at inria.fr> wrote:

> Dear all
> for teaching purposes, I am looking for snippets of code that, when run,
> exhibit some non-sc behaviours allowed by the Java memory model.
> I know Jaroslav examples (eg. those in
> http://groups.inf.ed.ac.uk/request/jmmexamples.pdf  or
> http://www.inf.ed.ac.uk/publications/online/1252.pdf ) but in those
> examples the non-sc behaviours can be spotted only by analysing the
> generated assembly code (looking at the bytecode is not enough as it is the
> JIT that performs the “surprising” optimisation), and the sc-behaviours are
> not practically observable executing the code.
> I can e.g. show that Dekker mutual exclusion algorithm using
> non-volatile/atomic variables (as implemented in:
> http://tinyurl.com/qylvjfl ) , can deadlock; my guess is that
>   if (turn==other) while (turn==other) ;
> is JIT optimised into
>   if (turn==other) while (true) ;
> but I cannot write a simpler snippet of code that makes this observable.
> Does any of you have suggestions or snippets of code that highlighlits
> non-sc behaviours for racy Java programs (possibly on a recent HotSpot VM)?
> Thank you in advance.
> Best
> -francesco

More information about the jmm-dev mailing list