How do you perform secure retrieval if the current operation is on a parallel hash map? (same as putIfAbsent)
Bad example, not very safe thread (check, then follow the action):
ConcurrentMap<String, SomeObject> concMap = new ... //... many putIfAbsent and remove operations public boolean setOption(String id, Object option){ SomeObject obj = concMap.get(id); if (obj != null){ //what if this key has been removed from the map? obj.setOption(option); return true; } // in the meantime a putIfAbsent may have been called on the map and then this //setOption call is no longer correct return false; }
Another bad example might be:
public boolean setOption(String id, Object option){ if (concMap.contains(id)){ concMap.get(id).setOption(option); return true; } return false; }
It is desirable that there are no bottlenecks for adding, deleting and receiving operations by synchronizing them.
thanks
source share