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

Francesco Zappa Nardelli francesco.zappa_nardelli at inria.fr
Thu Jan 15 17:31:19 UTC 2015


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