[concurrency-interest] 3 rules of final field semantics

Ruslan Cheremin cheremin at gmail.com
Mon Dec 26 05:46:16 EST 2011


It seems for me that your first example (rule 2) is incorrect. Final
field itself does not make any additional ordering guarantee for class
it contains in -- it gives ordering guarantee only for read going
through this final field.

Here

v.afield = 1; x.finalField = v; ... ; sharedRef = x;

you really can't reorder first two assignments with reference
publication. But your case is more complex actually:

local _v = new V(); _v.afield=1; x.finalField=_v; x.v=_v;sharedRef=x;

It seems for me that nothing prevent JIT to reorder (x.v = _x) after
(sharedRef=x) -- so you still can see  rule2.v = null, or even (not
sure about it) rule2.v.afield=0(default value)



2011/12/26 Mohan Radhakrishnan <radhakrishnan.mohan at gmail.com>:
> After the recent discussion about final fields I decided to look up
> the rules in the JMM.
>
> It looks like rule 1 is in 17.5 Final Field Semantics with an example.
>
> Do the programs shown below exemplfify rule 2 and 3 assuming one
> thread calls writer and the other calls reader? The sentences
> describing the rules are a little bit complex.
>
>
> Similarly, you cannot reorder either of the first two with the third
> assignment in:
>      v.afield = 1; x.finalField = v; ... ; sharedRef = x;
>
> (e.g)
>
> public class FinalRule2 {
>
>        private final V finalField;
>
>        private V v;
>
>        public static FinalRule2 rule2;
>
>        public FinalRule2() {
>
>                v.afield = 1;
>
>                finalField = v;
>
>        }
>
>        public static void writer(){
>
>                rule2 = new FinalRule2(); //sharedRef
>        }
>
>        public static void reader(){
>                if( null != rule2 ){
>                        System.out.println( rule2.v.afield );
>                }
>        }
> }
>
> class V{
>        int afield;
> }
>
>
> The initial load (i.e., the very first encounter by a thread) of a
> final field cannot be reordered with the initial load of the reference
> to the object containing the final field. This comes into play in:
>      x = sharedRef; ... ; i = x.finalField;
>
>
> (e.g)
>
> public class FinalRule3 {
>
>        private final int finalField;
>
>        private int i;
>
>        public static FinalRule3 rule3;
>
>        private FinalRule3 x;
>
>        public FinalRule3(){
>
>                finalField = 1;
>        }
>
>        public static void writer(){
>
>                rule3 = new FinalRule3();
>                x = rule3;                  //sharedRef
>        }
>
>        public static void reader(){
>                        if( null != rule3 ){
>                                int i = x.finalField;
>                                System.out.println( i );
>                        }
>        }
> }
>
> Thanks,
> Mohan
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest



More information about the Concurrency-interest mailing list