These are not just links. As a rule, without the volatile other threads can observe a new reference to the object, but observe the object in a partially constructed state. In general, it is not easy to find out, even after consulting the documentation, which objects are safe for publication on a data race. It is interesting to note that JLS guarantees this for thread-safe immutable objects , therefore, if these two properties are indicated in the documents, this should be sufficient.
ConcurrentHashMap is obviously not an immutable object, so it is not used, and the documents do not say anything about publishing as a result of data race. By carefully checking the source code, we can conclude that it is really safe, but I would not recommend relying on such conclusions if this property were not clearly documented.
source share