简体   繁体   English

在并发 HashMap 中重新散列期间检索

[英]Retrieval during Rehashing in Concurrent HashMap

I have read about the implementation of Concurrent HashMaps which states "ConcurrentHashMap does not block when performing retrieval operations, and there is no locking for the usual operations".我已经阅读了 Concurrent HashMaps 的实现,其中指出“ConcurrentHashMap 在执行检索操作时不会阻塞,并且通常操作没有锁定”。 So if let's say we call put() method and get() method simultaneously.因此,假设我们同时调用 put() 方法和 get() 方法。 If put() performs rehashing don't we need to synchronize the get() method.如果 put() 执行重新散列,我们不需要同步 get() 方法。 The get() method will determine the bucket using hash % N (where N is size of the hashmap). get() 方法将使用 hash % N (其中 N 是哈希图的大小)确定存储桶。 Shouldn't the bucket calculation method of get() get impacted because of rehashing? get() 的桶计算方法不应该因为重新散列而受到影响吗?

Rehashing only occurs when re-sizing the hash table.重新散列仅在重新调整 hash 表的大小时发生。

If get() is called during the re-size operation, then the put() hasn't completed yet, so the get() won't see the new value, which is as it should be.如果在 re-size 操作期间调用get() ,则put()尚未完成,因此get()将看不到新值,这是应该的。

Remember, the table bins are in an array, and arrays are fixed-size, so re-sizing means an entirely new hash table.请记住,表格箱是一个数组,并且 arrays 是固定大小的,因此重新调整大小意味着一个全新的 hash 表格。 That new hash table is not available to other threads until the re-size has completed.在重新调整大小完成之前,其他线程无法使用新的 hash 表。 Until then, all other threads calling get() will see the old hash table.在那之前,所有其他调用get()的线程将看到旧的 hash 表。

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

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