This seems wrong.
static ConcurrentHashMap k; //multiple threads have access to k
X o = k.get("LL");
o.a = 6;
If multiple threads access k concurrently, and get k(“LL”), then update (o.a = #) without k.put(“ll”,o), without synchronizing on ‘o’, or on ‘k’ what happens?
A ConcurrentMap has conditional operations that guarantee atomic insert/removal and replacement of key/value pairs. Additionally, accessing a ConcurrentMap creates a happens-before relationship so you can make certain guarantees about the ordering of your code.
In the code presented, the line:
accesses the current X value for the key “LL”. The next line modifies the a property. Without knowing the implementation of X, this is Java so we know that there is no method call here. If (and only if) the a property is marked as volatile then some subsequent code accessing the X at “LL” will see the a value as 6. If it isn’t volatile then there are no guarantees at all. They will probably see 6, particularly on an SMP x86 box, with not many threads doing much at the time. In production, on a big NUMA box, they are less likely to. Mutability brings with it all sorts of complications and difficulty.
Generally, you’ll find it is easier to reason about the state the map is in if you use immutable keys AND values.