简体   繁体   English

ConcurrentHashMap 中的键级锁定

[英]Key level locking in ConcurrentHashMap

I understand that the ConcurrentHashMap uses bucket level locking instead of locking the entire map object on modify operations.我了解ConcurrentHashMap使用存储桶级别锁定,而不是在修改操作时锁定整个地图对象。 This allows two threads trying to modify keys in different buckets to make the modification simultaneously.这允许尝试修改不同存储桶中的键的两个线程同时进行修改。 But if threads are trying to modify two different keys in the same bucket then only one will be allowed at a time.但是如果线程试图修改同一个桶中的两个不同的键,那么一次只允许一个。

I want to understand, what is the challenge in implementing key level locking in which threads trying to modify different keys are always allowed simultaneous modification.我想了解,实现密钥级别锁定的挑战是什么,其中始终允许同时修改尝试修改不同密钥的线程。 SQL DBs implement row level locking. SQL DB 实现行级锁定。 How are they able to do it efficiently?他们如何能够有效地做到这一点? What is the cost of increasing the number of locks/concurrencyLevel in ConcurrentHashMap from the default (which I believe is 16) to a higher number?将 ConcurrentHashMap 中的锁/并发级别数量从默认值(我相信是 16)增加到更高数量的成本是多少?

Operations locking everything操作锁定一切

Even if you lock buckets, you might still have operations that lock need to the whole data structure.即使您锁定了存储桶,您可能仍然需要锁定整个数据结构的操作。 For example, you might need to resize the Map which requires you to lock the whole data structure (or all buckets) before being able to recreate the data structure.例如,您可能需要调整Map的大小,这需要您在重新创建数据结构之前锁定整个数据结构(或所有存储桶)。

Acquiring multiple locks获取多个锁

Aside from that, you may even get worse performance when locking buckets instead of the whole data structure.除此之外,在锁定存储桶而不是整个数据结构时,您甚至可能会获得更差的性能。 For example, you might have frequent operations that require acquiring multiple locks (eg iterating over the whole data structure needs to make sure nothing writes to any part of it in the meantime).例如,您可能有频繁的操作需要获取多个锁(例如,遍历整个数据结构需要确保同时没有任何内容写入其中的任何部分)。 Needing to check multiple locks is a performance cost which increases with the number of locks to check.需要检查多个锁是一种性能成本,它会随着要检查的锁的数量而增加。

When acquiring multiple locks together, you might also run into deadlocks.当同时获取多个锁时,您也可能会遇到死锁。

Locks are not free锁不是免费的

Furthermore, locks are costly.此外,锁是昂贵的。 They require additional memory.它们需要额外的内存。 If you have lots and lots of buckets, you need lots of locks as well which can cumulate This gets worse the more locks you have.如果你有很多很多的桶,你也需要很多锁,这些锁会累积起来。你拥有的锁越多,情况就越糟。

Faulty implementations错误的实现

Lastly, it complicates your implementation and is error-prone as you might introduce logic errors that are hard to debug.最后,它会使您的实现复杂化并且容易出错,因为您可能会引入难以调试的逻辑错误。 While ConcurrentHashMap likely doesn't have those errors, your own implementations might.虽然ConcurrentHashMap可能没有这些错误,但您自己的实现可能。

Final notes最后的笔记

Note that these things vary from application to application/depending on your use-case.请注意,这些事情因应用程序而异/取决于您的用例。 If you change anything for better performance/concurrency, check before and after your changes and verify whether or not it got better.如果您更改任何内容以获得更好的性能/并发性,请在更改前后检查并验证它是否变得更好。

If you data structure (or whatever you need to lock) is not frequently contended, you might be better off with a single lock.如果您的数据结构(或您需要锁定的任何东西)不经常争用,那么使用单个锁可能会更好。 Bucketing locks is only useful when you have frequent concurrent operations on different buckets from different threads.只有当您对来自不同线程的不同存储桶进行频繁的并发操作时,存储桶锁才有用。

Disclaimer: This is just what I came up with, there may be other problems/difficulties.免责声明:这只是我想出的,可能还有其他问题/困难。

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

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