ConcurrentDictionary类
表示可由多个线程同时访问的键/值对的线程安全集合。
命名空间: System.Collections.Concurrent
程序集: mscorlib(位于 mscorlib.dll)
System.Object
System.Collections.Concurrent.ConcurrentDictionary
语法:
[SerializableAttribute]
[ComVisibleAttribute(false)]
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true,
ExternalThreading = true)]
public class ConcurrentDictionary : IDictionary,
ICollection>, IEnumerable>,
IEnumerable, IDictionary, ICollection, IReadOnlyDictionary,
IReadOnlyCollection>
类型参数
TKey
字典中的键的类型。
TValue
字典中的值的类型。
名称 | 说明 | |
---|---|---|
ConcurrentDictionary() | 初始化 ConcurrentDictionary 类的新实例,该实例为空,具有默认的并发级别和默认的初始容量,并为键类型使用默认比较器。 | |
ConcurrentDictionary(IEnumerable>) | 初始化 ConcurrentDictionary 类的新实例,该实例包含从指定的 IEnumerable 中复制的元素,具有默认的并发级别和默认的初始容量,并为键类型使用默认比较器。 | |
ConcurrentDictionary(IEnumerable>,IEqualityComparer) | 初始化 ConcurrentDictionary 类的新实例,该实例包含从指定的 IEnumerable 中复制的元素,具有默认的并发级别和默认的初始容量,并使用指定的 IEqualityComparer。 | |
ConcurrentDictionary(IEqualityComparer) | 初始化 ConcurrentDictionary 类的新实例,该实例为空,具有默认的并发级别和容量,并使用指定的 System.Collections.Generic.IEqualityComparer。 | |
ConcurrentDictionary(Int32,IEnumerable>,IEqualityComparer) | 初始化 ConcurrentDictionary 类的新实例,该实例包含从指定的 IEnumerable 中复制的元素并使用指定的 System.Collections.Generic.IEqualityComparer。 | |
ConcurrentDictionary(Int32,Int32) | 初始化 ConcurrentDictionary 类的新实例,该实例为空,具有指定的并发级别和容量,并为键类型使用默认比较器。 | |
ConcurrentDictionary(Int32,Int32,IEqualityComparer) | 初始化 System.Collections.Concurrent.ConcurrentDictionary 类的新实例,该实例为空,具有指定的并发级别和指定的初始容量,并使用指定的 System.Collections.Generic.IEqualityComparer。 |
名称 | 说明 | |
---|---|---|
Count | 获取包含在 System.Collections.Concurrent.ConcurrentDictionary 中的键/值对的数目。 | |
IsEmpty | 获取一个值,该值指示 System.Collections.Concurrent.ConcurrentDictionary 是否为空。 | |
Item[TKey] | 获取或设置与指定的键关联的值。 | |
Keys | 获得一个包含 System.Collections.Generic.Dictionary 中的键的集合。 | |
Values | 获取包含 System.Collections.Generic.Dictionary 中的值的集合。 |
名称 | 说明 | |
---|---|---|
AddOrUpdate(TKey,TValue,Func) | 如果该键不存在,则将键/值对添加到 ConcurrentDictionary 中;如果该键已经存在,则通过使用指定的函数更新 ConcurrentDictionary 中的键/值对。 | |
AddOrUpdate(TKey,Func,Func) | 如果该键不存在,则使用指定函数将键/值对添加到 ConcurrentDictionary;如果该键已存在,则使用该函数更新 ConcurrentDictionary 中的键/值对。 | |
Clear() | 将所有键和值从 ConcurrentDictionary 中移除。 | |
ContainsKey(TKey) | 确定是否 ConcurrentDictionary 包含指定键。 | |
Equals(Object) | 确定指定的对象是否等于当前对象。(继承自 Object。) | |
Finalize() | 在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。(继承自 Object。) | |
GetEnumerator() | 返回循环访问 ConcurrentDictionary 的枚举数。 | |
GetHashCode() | 作为默认哈希函数。(继承自 Object。) | |
GetOrAdd(TKey,TValue) | 如果该键不存在,则将键/值对添加到 ConcurrentDictionary 中。 | |
GetOrAdd(TKey,Func) | 如果该键不存在,则通过使用指定的函数将键/值对添加到 ConcurrentDictionary 中。 | |
GetType() | ||
MemberwiseClone() | ||
ToArray() | 将 System.Collections.Concurrent.ConcurrentDictionary 中存储的键和值对复制到新数组中。 | |
ToString() | 返回表示当前对象的字符串。(继承自 Object。) | |
TryAdd(TKey,TValue) | 尝试将指定的键和值添加到 System.Collections.Concurrent.ConcurrentDictionary 中。 | |
TryGetValue(TKey,TValue) | 尝试从 System.Collections.Concurrent.ConcurrentDictionary 获取与指定的键关联的值。 | |
TryRemove(TKey,TValue) | 尝试从 System.Collections.Concurrent.ConcurrentDictionary 中移除并返回具有指定键的值。 | |
TryUpdate(TKey,TValue,TValue) | 将指定键的现有值与指定值进行比较,如果相等,则用第三个值更新该键。 |
对于非常大 ConcurrentDictionary 对象时,您可以将最大数组大小增加到 2 千兆字节 (GB) 的 64 位系统上通过将配置元素设置为 true 在运行时环境中。
说明 |
---|
ConcurrentDictionary 实现 IReadOnlyCollection 和 IReadOnlyDictionary 开头的接口 .NET Framework 4.6; 在以前版本的.NET Framework 中, ConcurrentDictionary 类未实现这些接口。 |
如 System.Collections.Generic.Dictionary 类, ConcurrentDictionary 实现 IDictionary 接口。 此外, ConcurrentDictionary 提供了几种方法中添加或更新键/值对在字典中下, 表中所述。
任务 | 使用此方法 | 用法说明 |
---|---|---|
如果它尚不在字典中存在向字典中,添加新的密钥 | 如果当前不在字典中存在该键,此方法将添加指定的键/值对。 该方法返回 true 或 false 具体取决于是否已添加新对。 | |
如果该注册表项具有特定值,更新为现有键在字典中值 | 此方法检查是否密钥具有指定的值,如果它存在,则用新值更新该键。 它相当于 CompareExchange 方法,但它的用于字典的元素。 | |
无条件地将键/值对存储在字典中,覆盖已存在的键的值 | 索引器的资源库︰ dictionary[key] = newValue | |
将键/值对添加到字典中,或如果键已存在,更新基于键的现有值的键的值 | - 或 - | AddOrUpdate(TKey,Func,Func) 接受的键和两个委托。 如果键在字典; 中不存在,则使用第一个委托它接受键并返回应添加的键的值。 如果该键不存在; 它使用第二个委托它接受的键和其当前值,并返回应为项设置的新值。 AddOrUpdate(TKey,TValue,Func) 接受键、 值要添加,以及更新委托。 这是与以前的重载中,相同之处在于它不使用委托来添加的键。 |
获取此键在字典中,向字典中添加值并将其返回如果该键不存在的值 | - 或 - | 这些重载提供延迟初始化为键/值对在字典中,添加的值,仅当不存在。 GetOrAdd(TKey,TValue) 采用键不存在要添加的值。 GetOrAdd(TKey,Func) 将一个委托,可将生成的值,如果键不存在。 |
所有这些操作是原子,而且都是线程安全方面的所有其他操作 ConcurrentDictionary 类。 唯一的例外是,它是接受一个委托,委托的方法 AddOrUpdate 和 GetOrAdd。 修改和向字典中,写入操作 ConcurrentDictionary 使用细粒度锁定,以确保线程安全。 (dictionary 上的读取操作是以无锁方式执行。) 但是,外部锁以避免出现问题而引起执行下一个锁未知的代码调用这些方法的委托。 因此,这些委托执行的代码并不受到的原子性操作。
下面的示例演示如何构造 ConcurrentDictionary 对象。
class CD_Ctor
{ // Demonstrates:
// ConcurrentDictionary ctor(concurrencyLevel,
// initialCapacity)
// ConcurrentDictionary[TKey]
static void Main()
{
// We know how many items we want to insert into the ConcurrentDictionary.
// So set the initial capacity to some prime number above that, to ensure that
// the ConcurrentDictionary does not need to be resized while initializing it.
int NUMITEMS = 64;
int initialCapacity = 101;
// The higher the concurrencyLevel, the higher the theoretical number of operations
// that could be performed concurrently on the ConcurrentDictionary. However, global
// operations like resizing the dictionary take longer as the concurrencyLevel rises.
// For the purposes of this example, we'll compromise at numCores * 2.
int numProcs = Environment.ProcessorCount;
int concurrencyLevel = numProcs * 2;
// Construct the dictionary with the desired concurrencyLevel and initialCapacity
ConcurrentDictionary cd = new ConcurrentDictionary(concurrencyLevel
, initialCapacity); // Initialize the dictionary
for (int i = 0; i < NUMITEMS; i++) cd[i] = i * i;
Console.WriteLine("The square of 23 is {0} (should be {1})",
cd[23], 23 * 23);
}
}
备注:转自https://msdn.microsoft.com/zh-cn/library/dd287191(v=vs.110).aspx
转载于:https://blog.51cto.com/jiaojusuimu/1880317