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>
することで、64 ビット システムで最大配列サイズを 2 ギガバイト (GB) に true
増やすことができます。
注意
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 似ていますが、ディクショナリ要素に使用される点が除きます。 |
キーと値のペアを無条件にディクショナリに格納し、既に存在するキーの値を上書きします | インデクサーのセッター: 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> に格納されているキー/値ペアの数を取得します。 |
IsEmpty |
ConcurrentDictionary<TKey,TValue> が空かどうかを示す値を取得します。 |
Item[TKey] |
指定されたキーに関連付けられている値を取得または設定します。 |
Keys |
Dictionary<TKey,TValue> 内のキーを格納しているコレクションを取得します。 |
Values |
Dictionary<TKey,TValue> 内の値を含むコレクションを取得します。 |
メソッド
明示的なインターフェイスの実装
拡張メソッド
適用対象
スレッド セーフ
のすべてのパブリック メンバーとプロテクト メンバー ConcurrentDictionary<TKey,TValue> はスレッド セーフであり、複数のスレッドから同時に使用できます。 ただし、拡張メソッドを含む、実装されているインターフェイス ConcurrentDictionary<TKey,TValue> の 1 つを介してアクセスされるメンバーは、スレッド セーフであるとは限らず、呼び出し元による同期が必要になる場合があります。
こちらもご覧ください
.NET
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示