简体   繁体   English

ConcurrentHashMap更新了多个条目

[英]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. 我有一种情况,在ConcurrentHashMap中我存储一些属性,如IP地址和端口或罚款和代码 - 2个或更多相互连接的条目。

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. 我可以使用ReadWriteLock块包装get / put方法,但感觉不对:)我没有使用ConcurrentHashMap来编写额外的锁。

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. 使用带外部同步的简单HashMap

  • Use a different type for the Map values, which goes the pair of items that must be considered together. Map值使用不同的类型,这是必须一起考虑的项目对。

I think, You can use different way, without ConcurrentHashMap. 我想,你可以使用不同的方式,没有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. 例如:更新操作更高然后获取操作,您可以使用PriorityBlockingQueue ,例如: HashSet来存储对象而不重复。 Then You are sure Your actual properties is the newest one. 那么你确定你的实际属性是最新的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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