In short, you do not need the card to be thread safe unless the readings are destructive and the map link is safely published to the client.
In this example, there are two important things going on before the relationship established here. Publication of the final field (if and only if the population runs inside the constructor, and the link does not flow outside the constructor) and calls to start the threads.
Everything that changes the card after these calls when the client reads from the card is not published securely.
We have, for example, CopyOnWriteMap , which contains a non-stream card underlying, which is copied every time. This happens as quickly as possible in situations where there are many other reads than writes (an example of configuration caching is a good example).
However, if the intention is really not to change the map, setting an unchanged version of the map in the field is always the best way, as this ensures that the client sees the right thing.
Finally, there are some Map implementations that have destructive readings, such as LinkedHashMap with access order, or WeakHashMap , where entries may disappear. These types of cards should be accessed sequentially.
Jed wesley-smith
source share