[concurrency-interest] Simulating effects of final fields with memory fences

Peter Levart peter.levart at gmail.com
Mon Jun 18 06:12:51 EDT 2018


Hello,

I would like to ask the list one concurrency related question. Say you 
have a class with a final reference field:

public class X {
     private final Y y;

     public X() {
         y = ...;
     }

     public void m() {
         ... use y ...
     }
}

And that Y is either an immutable or properly synchronized type so "use 
y" never has any data races.

Objects of type X can be published via data races, but final modifier on 
field y guarantees that the field will never be observed as 
uninitialized (null) in method m.

Now say that class X is modified in the following way:

public class X {
     private Y y;

     public X() {
         y = ...;
         VarHandle.storeStoreFence();
     }

     public void m() {
         VarHandle.loadLoadFence();
         ... use y ...
     }
}

- final modifier is removed, storeStoreFence is added as the final 
action in constructor, loadLoadFence is added as the first action in method.

Would every guarantee that holds for original class X still hold for 
modified class X ?


Thanks, Peter



More information about the Concurrency-interest mailing list