I've recently stumbled upon an article titled Synchronize access to mutable fields. It claims that:
For example, in a multi-threaded environment, all
get
andset
methods for mutable fields should usually besynchronized
methods. This includes primitive fields.
My question is why? What would be the use of synchronizing getId
method? Or what could happen if I don't synchronize it.
For example I pass a UserContext
to a Spring Service function and call getUserId
within the function. Could this be a problem if getUserId
is not synchronized?