同步对 ConcurrentMap 中给定键的访问
我经常想要访问(并且可能添加/删除)给定 ConcurrentMap 的元素,以便一次只有一个线程可以访问任何单个键。最好的方法是什么?同步密钥本身不起作用:其他线程可能通过 equal
实例访问相同的密钥。
如果答案仅适用于 guava MapMaker 构建的地图,那就足够了
。
I often enough want to access (and possibly add/remove) elements of a given ConcurrentMap
so that only one thread can access any single key at a time. What is the best way to do this? Synchronizing on the key itself doesn't work: other threads might access the same key via an equal
instance.
It's good enough if the answer only works with the maps built by guava MapMaker
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这里查看一个简单的解决方案 基于简单 Java 名称的锁?
编辑:这解决方案从解锁到锁定具有明确的先发生关系。然而,现在已撤回的下一个解决方案却没有。
ConcurrentMap
javadoc 太轻,无法保证这一点。(撤回)如果你想将你的映射重新用作锁池,
这会很混乱,因为我们将映射重新用于两个不同的目的。最好将锁池作为一个单独的数据结构,而将map简单地作为kv存储。
See a simple solution here Simple Java name based locks?
EDIT: This solution has a clear happens-before relation from unlock to lock. However, the next solution, now withdrawn, doesn't. The
ConcurrentMap
javadoc is too light to guaranteed that.(Withdrawn) If you want to reuse your map as a lock pool,
This is quite messy because we reuse the map for two difference purposes. It's better to have lock pool as a separate data structure, leave map simply as the k-v storage.
难道你不能创建自己的类来扩展并发映射吗?
重写
get(Object key)
方法,以便它检查请求的密钥对象是否已被另一个线程“检出”?您还必须在并发映射中创建一个新方法,将项目“返回”到映射,以便它们再次可供另一个线程使用。
Can't you just create you own class that extends concurrentmap.
Override the
get(Object key)
method, so it checks if the requested key object is already 'checked out' by another thread ?You'll also have to make a new method in your concurrentmap that 'returns' the items to the map, so they are available again to another thread.