解决方法
您可能希望同步地图的初始化,以确保没有线程在完全初始化之前访问它,并确保加载到地图中的值都可见.
编辑:本来我完全忽略了地图如何初始化的问题.阅读one of the Pugh articles(再次)似乎地图真的需要是最终的,以便初始化数据变得可见:
The ability to see the correctly constructed value for the field is nice,but if the field itself is a reference,then you also want your code to see the up to date values for the object (or array) to which it points. If your field is a final field,this is also guaranteed. So,you can have a final pointer to an array and not have to worry about other threads seeing the correct values for the array reference,but incorrect values for the contents of the array. Again,by “correct” here,we mean “up to date as of the end of the object’s constructor”,not “the latest value available”.
有一个条件列出强制“先发生”的关系,在Java规范中给出,我应该在这里引用它(或者如果有人在他的答案中我会投票).静态变量和持有者成语当然是一种方式.问题是相当广泛的,因为它没有指定地图如何初始化,如果你发布一个问题描述你如何提出初始化,你可能会得到一个更直接有用的答案.