23.4.1 Immutable Data
Actually, thread synchronization is necessary only for shared mutable data, i.e., data that may change during its lifetime. With shared immutable data that will not change, it’s not possible for a thread to see old or incorrect values as a result of another thread’s manipulation of that data.
When you share immutable data across threads, declare the corresponding data fields final to indicate that the values of the variables will not change after they’re initialized. This prevents accidental modification of the shared data, which could compromise thread safety. Labeling object references as final indicates that the reference will not change, but it does not guarantee that the referenced object is immutable—this depends ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access