If each component is already thread-safe in a composite object, it needs to be added with an additional "thread-safe layer", depending on the situation.
Final can modify an attribute that is not copied, as long as it is assigned to a static code block or constructor.
Stand-alone state variables
We can also delegate the security of threads to multiple state variables, as long as they are independent of each other, that is, the combined class does not add any invariants to the multiple state variables it contains.
If there are some invariance conditions between the state variables, it is not so simple. For example, there are two properties in a class, and the condition to be satisfied is that the value of one property is less than the value of another. This is typically done after the first check, if there is not enough locking mechanism to ensure the atomicity of the operation, this delegation is not enough to achieve the security of the thread. In this case, each class must provide its own locking mechanism to ensure that these compound operations are atomic operations.
A variable can be declared volatile only if it participates in an invariant condition that contains other state variables.
For example, to return the coordinates of a point, you need to get the value of the coordinate x, and the value of the coordinate y, which may result in a different value: The point never appears on this coordinate.
Java Concurrency Programming (12) thread-Safe delegation