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
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> 的物件,您可以將運行時間環境中的組態專案設定 <gcAllowVeryLargeObjects>
為 true
,將64位系統上的陣列大小上限增加到2 GB () 。
注意
ConcurrentDictionary<TKey,TValue>會IReadOnlyCollection<T>實作 從 .NET Framework 4.6 開始的 和 IReadOnlyDictionary<TKey,TValue> 介面;在舊版的 .NET Framework 中,類別ConcurrentDictionary<TKey,TValue>並未實作這些介面。
如同 類別,ConcurrentDictionary<TKey,TValue>實作 System.Collections.Generic.Dictionary<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> 存取的成員,包括擴充方法,不保證是安全線程,而且可能需要由呼叫端同步處理。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應