ConcurrentDictionary<TKey,TValue> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示可由多个线程同时访问的键/值对的线程安全集合。
generic <typename TKey, typename TValue>
public ref class ConcurrentDictionary : System::Collections::Generic::ICollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IDictionary<TKey, TValue>, System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IReadOnlyCollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IReadOnlyDictionary<TKey, TValue>, System::Collections::IDictionary
generic <typename TKey, typename TValue>
public ref class ConcurrentDictionary : System::Collections::Generic::ICollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IDictionary<TKey, TValue>, System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::IDictionary
public class ConcurrentDictionary<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>, System.Collections.IDictionary
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class ConcurrentDictionary<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.IDictionary
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class ConcurrentDictionary<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>, System.Collections.IDictionary
public class ConcurrentDictionary<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.IDictionary
type ConcurrentDictionary<'Key, 'Value> = class
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IEnumerable
interface IDictionary<'Key, 'Value>
interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
interface IReadOnlyDictionary<'Key, 'Value>
interface ICollection
interface IDictionary
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type ConcurrentDictionary<'Key, 'Value> = class
interface IDictionary<'Key, 'Value>
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IDictionary
interface ICollection
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type ConcurrentDictionary<'Key, 'Value> = class
interface IDictionary<'Key, 'Value>
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IEnumerable
interface IDictionary
interface ICollection
interface IReadOnlyDictionary<'Key, 'Value>
interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
type ConcurrentDictionary<'Key, 'Value> = class
interface IDictionary<'Key, 'Value>
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IDictionary
interface ICollection
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type ConcurrentDictionary<'Key, 'Value> = class
interface IDictionary<'Key, 'Value>
interface IDictionary
interface IReadOnlyDictionary<'Key, 'Value>
interface ICollection<KeyValuePair<'Key, 'Value>>
interface seq<KeyValuePair<'Key, 'Value>>
interface IEnumerable
interface ICollection
interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
Public Class ConcurrentDictionary(Of TKey, TValue)
Implements ICollection(Of KeyValuePair(Of TKey, TValue)), IDictionary, IDictionary(Of TKey, TValue), IEnumerable(Of KeyValuePair(Of TKey, TValue)), IReadOnlyCollection(Of KeyValuePair(Of TKey, TValue)), IReadOnlyDictionary(Of TKey, TValue)
Public Class ConcurrentDictionary(Of TKey, TValue)
Implements ICollection(Of KeyValuePair(Of TKey, TValue)), IDictionary, IDictionary(Of TKey, TValue), IEnumerable(Of KeyValuePair(Of TKey, TValue))
类型参数
- TKey
字典中的键的类型。
- TValue
字典中的值的类型。
- 继承
-
ConcurrentDictionary<TKey,TValue>
- 派生
- 属性
- 实现
-
ICollection<KeyValuePair<TKey,TValue>> IDictionary<TKey,TValue> IEnumerable<KeyValuePair<TKey,TValue>> IEnumerable<T> IReadOnlyCollection<KeyValuePair<TKey,TValue>> IReadOnlyDictionary<TKey,TValue> ICollection IDictionary IEnumerable
示例
以下示例演示如何构造 ConcurrentDictionary<TKey,TValue> 对象。
class CD_Ctor
{
// Demonstrates:
// ConcurrentDictionary<TKey, TValue> ctor(concurrencyLevel, initialCapacity)
// ConcurrentDictionary<TKey, TValue>[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<int, int> cd = new ConcurrentDictionary<int, int>(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);
}
}
// Demonstrates:
// ConcurrentDictionary<TKey, TValue> ctor(concurrencyLevel, initialCapacity)
// ConcurrentDictionary<TKey, TValue>[TKey]
// 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.
let NUMITEMS = 64
let 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.
let numProcs = Environment.ProcessorCount
let concurrencyLevel = numProcs * 2
// Construct the dictionary with the desired concurrencyLevel and initialCapacity
let cd = ConcurrentDictionary<int, int>(concurrencyLevel, initialCapacity)
// Initialize the dictionary
for i = 0 to NUMITEMS - 1 do
cd[i] <- i * i
printfn $"The square of 23 is {cd[23]} (should be {23 * 23})"
Imports System.Collections.Concurrent
Imports System.Threading.Tasks
Class CD_Ctor
' Demonstrates:
' ConcurrentDictionary<TKey, TValue> ctor(concurrencyLevel, initialCapacity)
' ConcurrentDictionary<TKey, TValue>[TKey]
Shared Sub 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.
Dim NUMITEMS As Integer = 64
Dim initialCapacity As Integer = 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.
Dim numProcs As Integer = Environment.ProcessorCount
Dim concurrencyLevel As Integer = numProcs * 2
' Construct the dictionary with the desired concurrencyLevel and initialCapacity
Dim cd As New ConcurrentDictionary(Of Integer, Integer)(concurrencyLevel, initialCapacity)
' Initialize the dictionary
For i As Integer = 0 To NUMITEMS - 1
cd(i) = i * i
Next
Console.WriteLine("The square of 23 is {0} (should be {1})", cd(23), 23 * 23)
End Sub
End Class
注解
对于非常大ConcurrentDictionary<TKey,TValue>的对象,通过在运行时环境中将配置元素true
设置为 ,可以在 64 位系统上将最大数组大小增加到 <gcAllowVeryLargeObjects>
2 千兆字节 (GB) 。
注意
ConcurrentDictionary<TKey,TValue>实现 IReadOnlyCollection<T> 从 .NET Framework 4.6 开始的 和 IReadOnlyDictionary<TKey,TValue> 接口;在以前版本的 .NET Framework中ConcurrentDictionary<TKey,TValue>, 类未实现这些接口。
与 类一 System.Collections.Generic.Dictionary<TKey,TValue> 样, ConcurrentDictionary<TKey,TValue> 实现 IDictionary<TKey,TValue> 接口。 此外, ConcurrentDictionary<TKey,TValue> 提供了几种用于在字典中添加或更新键/值对的方法,如下表所述。
要执行此操作 | 方法 | 使用注意事项 |
---|---|---|
向字典添加新键(如果字典中尚不存在) | TryAdd | 如果字典中当前不存在该键,则此方法将添加指定的键/值对。 方法返回 true 或 false ,具体取决于是否添加新对。 |
更新字典中现有键的值(如果该键具有特定值) | TryUpdate | 此方法检查密钥是否具有指定值,如果具有指定值,则使用新值更新密钥。 它类似于 CompareExchange 方法,只不过它用于字典元素。 |
无条件地将键/值对存储在字典中,并覆盖已存在的键的值 | 索引器的 setter: dictionary[key] = newValue |
|
将键/值对添加到字典,或者如果键已存在,请根据键的现有值更新键的值 | AddOrUpdate(TKey, Func<TKey,TValue>, Func<TKey,TValue,TValue>) - 或 - AddOrUpdate(TKey, TValue, Func<TKey,TValue,TValue>) |
AddOrUpdate(TKey, Func<TKey,TValue>, Func<TKey,TValue,TValue>) 接受密钥和两个委托。 如果字典中不存在键,则使用第一个委托;它接受密钥并返回应为键添加的值。 如果密钥确实存在,则使用第二个委托;它接受键及其当前值,并返回应为键设置的新值。 AddOrUpdate(TKey, TValue, Func<TKey,TValue,TValue>) 接受密钥、要添加的值和更新委托。 这与前面的重载相同,只不过它不使用委托添加密钥。 |
获取字典中某个键的值,将值添加到字典中,如果该键不存在,则返回该值 | GetOrAdd(TKey, TValue) - 或 - GetOrAdd(TKey, Func<TKey,TValue>) |
这些重载为字典中的键/值对提供延迟初始化,仅在值不存在时才添加该值。 GetOrAdd(TKey, TValue) 如果键不存在,则采用要添加的值。 GetOrAdd(TKey, Func<TKey,TValue>) 采用委托,该委托将在键不存在时生成值。 |
所有这些操作都是原子操作,对于类上的 ConcurrentDictionary<TKey,TValue> 所有其他操作,都是线程安全的。 唯一的例外是接受委托的方法,即 AddOrUpdate 和 GetOrAdd。 对于对字典的修改和写入操作, ConcurrentDictionary<TKey,TValue> 请使用精细锁定来确保线程安全。 (字典上的读取操作以无锁方式执行。) 但是,这些方法的委托在锁外部调用,以避免在锁下执行未知代码时可能出现的问题。 因此,这些委托执行的代码不受操作原子性的约束。
构造函数
属性
Comparer |
获取用于确定字典中的键是否相等的 IEqualityComparer<T>。 |
Count |
获取包含在 ConcurrentDictionary<TKey,TValue> 中的键/值对的数目。 |
IsEmpty |
获取一个值,该值指示 ConcurrentDictionary<TKey,TValue> 是否为空。 |
Item[TKey] |
获取或设置与指定的键关联的值。 |
Keys |
获得一个包含 Dictionary<TKey,TValue> 中的键的集合。 |
Values |
获取包含 Dictionary<TKey,TValue> 中的值的集合。 |
方法
显式接口实现
扩展方法
适用于
线程安全性
的所有公共成员和受保护成员 ConcurrentDictionary<TKey,TValue> 都是线程安全的,可以从多个线程并发使用。 但是,通过实现的接口 ConcurrentDictionary<TKey,TValue> 之一(包括扩展方法)访问的成员不能保证线程安全,并且可能需要由调用方同步。
另请参阅
反馈
提交和查看相关反馈