A common misconception is that shared references to immutable objects are immediately visible across multiple threads as soon as they are updated. For example, a developer can mistakenly believe that a class containing fields that refer only to immutable objects is itself immutable and consequently threa
Section 14.10.2, "Final Fields and Security" of Java Programming Language, Fourth Edition [JPL 2006] states:
The problem is that, while the shared object is immutable, the reference used to access the shared object is itself shared and often mutable. Consequently, a correctly synchronized program must synchronize access to that shared reference, but often programs do not do this, because programmers do not recognize the need to do it.
...
The getHelper()
method publishes the mutable helper
field. Because the Helper
class is immutable, it cannot be changed after it is initialized
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
ThreadSafe |
| CCE_SL_INCONSISTENT CCE_CC_CALLBACK_ACCESS CCE_SL_MIXED CCE_SL_INCONSISTENT_COL CCE_SL_MIXED_COL CCE_CC_UNSAFE_CONTENT | Implemented |
Bibliography
...