简体   繁体   中英

ConcurrentHashMap update of multiple entries

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

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.

I think, You can use different way, without ConcurrentHashMap.

If you want to add somethink like the priority of your operations. eg: update operations in higher then get operations, You can use PriorityBlockingQueue , and eg.: HashSet to storing objects without repetition. Then You are sure Your actual properties is the newest one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM