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))
// 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
Console.WriteLine("The square of 23 is {0} (should be {1})", cd(23), 23 * 23)
End Sub
End Class
對於非常大
ConcurrentDictionary<TKey,TValue>
的物件,您可以將執行時間環境中的組態專案
true
設定
<gcAllowVeryLargeObjects>
為 ,將 64 位系統上的陣列大小上限增加為 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>
請使用精細鎖定來確保執行緒安全。 (字典上的讀取作業是以無鎖定的方式執行。) 不過,這些方法的委派會在鎖定外部呼叫,以避免在鎖定下執行未知程式碼時可能發生的問題。 因此,這些委派所執行的程式碼不受作業的不可部分完成性所限制。
ToImmutableDictionary<TSource,TKey,TValue>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TValue>, IEqualityComparer<TKey>,
IEqualityComparer<TValue>)
列舉及轉換序列,並使用指定的索引鍵與值比較子產生其內容的不可變字典。
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,
TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,
TResult>, IEqualityComparer<TKey>)
依據指定的索引鍵選取器函式來群組序列的項目,並從每個群組及其索引鍵建立結果值。 索引鍵值是使用指定的比較子來進行比較,而每個群組的項目則都是利用指定的函式進行投影。
GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>,
Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,IEnumerable<TInner>,
TResult>)
根據索引鍵相等與否,將兩個序列的項目相互關聯,並群組產生的結果。 預設的相等比較子是用於比較索引鍵。
GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>,
Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,IEnumerable<TInner>,
TResult>, IEqualityComparer<TKey>)
根據索引鍵相等與否,將兩個序列的項目相互關聯,並群組產生的結果。 指定的
IEqualityComparer<T>
是用於比較索引鍵。
Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>,
Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,TInner,TResult>,
IEqualityComparer<TKey>)
根據相符索引鍵,將兩個序列的項目相互關聯。 指定的
IEqualityComparer<T>
是用於比較索引鍵。
執行緒安全性
的所有公用和受保護成員
ConcurrentDictionary<TKey,TValue>
都是安全線程,而且可以從多個執行緒同時使用。 不過,透過實作的其中一個介面
ConcurrentDictionary<TKey,TValue>
存取成員,包括擴充方法,不保證為安全線程,而且可能必須由呼叫端同步處理。
安全執行緒集合
作法:在 ConcurrentDictionary 中新增和移除項目