ConcurrentHashMap update of multiple entries

user2377971 :

i have a situation that in ConcurrentHashMap im storing some properties like ip address and port or penalty fee and code - 2 or more entries that are connected to each other.

Now i want to update those 2 entries atomically to avoid reading incorrect pairs.

So now im thinking about it and dont know simple sollution.

I could wrap get/put methods with ReadWriteLock blocks but that feels wrong :) I'm not using ConcurrentHashMap to write additional locks.

Other option is to merge those properties into one. Right now i'm leaning towards that option.

Other option is to have version in value object but then i need to check it every time and i dont want to do it :)

Is there some other sollution to that problem ?

Regards

Raedwald :

You can't atomically update multiple entries. You have two options:

  • Use a simple HashMap with external synchronization.

  • Use a different type for the Map values, which goes the pair of items that must be considered together.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=99568&siteId=1