ConcurrentHashMap

本文介绍了一种支持并发读取和更新操作的哈希表实现,详细阐述了其在检索、迭代器使用以及更新操作方面的特性和限制。包括并发控制策略、迭代器行为、更新限制和性能比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A version of Hashtable supporting concurrency for both retrievals and updates:

Retrievals

Retrievals may overlap updates. (This is the same policy as ConcurrentReaderHashMap.) Successful retrievals using get(key) and containsKey(key) usually run without locking. Unsuccessful retrievals (i.e., when the key is not present) do involve brief synchronization (locking). Because retrieval operations can ordinarily overlap with update operations (i.e., put, remove, and their derivatives), retrievals can only be guaranteed to return the results of the most recently completed operations holding upon their onset. Retrieval operations may or may not return results reflecting in-progress writing operations. However, the retrieval operations do always return consistent results -- either those holding before any single modification or after it, but never a nonsense result. For aggregate operations such as putAll and clear, concurrent reads may reflect insertion or removal of only some entries.

Iterators and Enumerations (i.e., those returned by keySet().iterator(), entrySet().iterator(), values().iterator(), keys(), and elements()) return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They will return at most one instance of each element (via next()/nextElement()), but might or might not reflect puts and removes that have been processed since they were created. They do not throw ConcurrentModificationException. However, these iterators are designed to be used by only one thread at a time. Passing an iterator across multiple threads may lead to unpredictable results if the table is being concurrently modified.

Updates

This class supports a hard-wired preset concurrency level of 32. This allows a maximum of 32 put and/or remove operations to proceed concurrently. This level is an upper bound on concurrency, not a guarantee, since it interacts with how well-strewn elements are across bins of the table. (The preset value in part reflects the fact that even on large multiprocessors, factors other than synchronization tend to be bottlenecks when more than 32 threads concurrently attempt updates.) Additionally, operations triggering internal resizing and clearing do not execute concurrently with any operation.

There is NOT any support for locking the entire table to prevent updates. This makes it imposssible, for example, to add an element only if it is not already present, since another thread may be in the process of doing the same thing. If you need such capabilities, consider instead using the ConcurrentReaderHashMap class.

Because of how concurrency control is split up, the size() and isEmpty() methods require accumulations across 32 control segments, and so might be slightly slower than you expect.

This class may be used as a direct replacement for java.util.Hashtable in any application that does not rely on the ability to lock the entire table to prevent updates. As of this writing, it performs much faster than Hashtable in typical multi-threaded applications with multiple readers and writers. Like Hashtable but unlike java.util.HashMap, this class does NOT allow null to be used as a key or value.

Implementation note: A slightly faster implementation of this class will be possible once planned Java Memory Model revisions are in place.

转载于:https://my.oschina.net/swearyd7/blog/177171

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值