Say I store a object in a hashmap that has a atomiclong as property.
Now I want to put a key in the hashmap if it doesn't exist, and update the value if it does.
When I get the object using the key, I will increment the property which is an atomic long.
What I need to understand is, what operation is thread-safe here?
Psuedo code:
HashMap hm = new HashMap
if(hm.containsKey(key1)) {
MyCounter counter = (MyCounter)hm.get(key1);
counter.incrementAndGet();
}
else {
MyCounter newCounter = new MyCounter();
newCounter.incrementAndGet();
hm.put(key1, newCounter);
}