简体   繁体   English

ConcurrentHashMap的get()与HashTable的get()

[英]ConcurrentHashMap's get() Vs HashTable's get()

I read that both ConcurrentHashMap 's get() method as well as HashTable 's get() is thread safe, even though former does not use synchronized keyword. 我读到ConcurrentHashMapget()方法和HashTableget()都是线程安全的,即使前者不使用synced关键字。 Why in HashTable 's get() method implementation synchronized keyword is required to make it thread safe but it is not required in case of ConcurrentHashMap 's get() method. 为什么在HashTableget()方法实现中需要使用synced关键字来使其具有线程安全性,但对于ConcurrentHashMapget()方法却不是必需的。

Broadly because HashTable and ConcurrentHashMap don't have exactly the same behavior and so actual results concerning concurrency. 大致上,因为HashTableConcurrentHashMap的行为不完全相同 ,因此涉及并发的实际结果。 For retrieval operations, the fact that ConcurrentHashMap doesn't lock the whole table makes ConcurrentHashMap to may not reflect the last updated value for the key as get() may overlap with put() / remove() operations : 对于检索操作, ConcurrentHashMap不会锁定整个表的事实使ConcurrentHashMap可能无法反映键的最后更新值,因为get()可能与put() / remove()操作重叠:

Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove). 检索操作(包括get)通常不会阻塞,因此可能与更新操作(包括put和remove)重叠。 Retrievals reflect the results of the most recently completed update operations holding upon their onset 检索反映了刚发生时最新完成的更新操作的结果

And when this delay doesn't matter in many use cases and also you can cope with that with explicit synchronized statements, you prefer to use ConcurrentHashMap over HashTable as it improves the overall Map performance for concurrent access. 而且,当这种延迟在许多用例中都无关紧要,并且您也可以使用显式同步语句来解决时,您更喜欢在HashTable上使用ConcurrentHashMap ,因为它可以提高并发访问的整体Map性能。

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

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