[concurrency-interest] Volatile/final and the JIT

Brian Goetz brian at briangoetz.com
Wed May 6 22:37:43 EDT 2009

> Different instances of this class may have different values assigned
> to the statistics field, but the code itself is static and, I would
> assume, optimized as such. 

This is not true.  There's nothing that says the JIT need only compile one 
version of a given method.  HotSpot will readily clone code where it can spot 
a slow/fast path, even based on deep inlining.  For example:

foo(Moo x) {

goo(Moo x) {
   if (x != null) { something expensive }

Here, calls to foo() may will be inlined (or inlined-cached), and in the 
branch where it is inlined, it can pull the null check all the way out to the 
caller, and have two completely different paths based on a condition that is 
way far down the tree.

More information about the Concurrency-interest mailing list