Please explain initialization safety as spelled out in Java memory model

白昼怎懂夜的黑 提交于 2019-11-28 19:33:36

Initialization safety provides for an object to be seen by an external thread in its fully constructed ( initialized ) state. The prerequisite is that the object should not be published prematurely ie. in its constructor. Once this is ensured , JMM requires certain behaviour for the fields that are declared as final. First , all final object fields are guarenteed to be seen by an external thread in its fully initialized state - this is not as trivial as it sounds -

Consider a class

class A{
   List list ;
   A() {  
      list = Arrays.asList(some init expressions that adds 10 elements to list);
    }

}

A thread that accesses the list of A's instance is not by default guaranteed to see 10 elements in that list. In fact , this thread can even see list as null. However , if list is declared final , then ,as required by JMM, the list must always appear to be initialized with 10 elements it it.

Secondly , this initialization guarantee is not limited to the final field itself, but is extended recursively to all objects referred by it. For example , if the list in the above example is a list of lists themselves , then the external thread is guaranteed to see the inner lists as fully initialized.

Note that nowhere are we using synchronized to achieve this safety in memory visibility ( happens-before relationship).

1. Initialization safety allows properly constructed immutable objects to be shared safely across threads without using synchronization, irrespective of even if they published using a data race.

2. Objects having final field, initialization safety prevent reordering any part of construction with the initial load of a reference to that object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!