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 メソッドに似ていますが、ディクショナリ要素に使用される点が異なります。 |
キーと値のペアを無条件にディクショナリに格納し、既に存在するキーの値を上書きします | インデクサーのセッター: 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>) は、キーと 2 つのデリゲートを受け入れます。 キーがディクショナリに存在しない場合は、最初のデリゲートが使用されます。キーを受け取り、キーに追加する必要がある値を返します。 キーが存在する場合は、2 番目のデリゲートが使用されます。キーとその現在の値を受け取り、キーに設定する必要がある新しい値を返します。 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>に含まれるキーと値のペアの数を取得します。 |
Is |
ConcurrentDictionary<TKey,TValue> が空かどうかを示す値を取得します。 |
Item[TKey] |
指定したキーに関連付けられている値を取得または設定します。 |
Keys |
Dictionary<TKey,TValue>内のキーを含むコレクションを取得します。 |
Values |
Dictionary<TKey,TValue>内の値を含むコレクションを取得します。 |
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
ConcurrentDictionary<TKey,TValue> のすべてのパブリック メンバーと保護されたメンバーはスレッド セーフであり、複数のスレッドから同時に使用できます。 ただし、拡張メソッドを含め、ConcurrentDictionary<TKey,TValue> が実装するインターフェイスのいずれかを介してアクセスされるメンバーは、スレッド セーフであるとは限らず、呼び出し元による同期が必要になる場合があります。
- Thread-Safe コレクションの
- 方法: ConcurrentDictionary から項目を追加および削除する
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。