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> 會從 .NET Framework 4.6 開始實作 IReadOnlyCollection<T> 和 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> 實作,包括擴充方法,不保證是安全線程,而且可能需要由呼叫端同步處理。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應