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비트 시스템에서 2GB(기가바이트)로 늘릴 수 있습니다.
메모
ConcurrentDictionary<TKey,TValue>는 IReadOnlyCollection<T> 및 IReadOnlyDictionary<TKey,TValue> 인터페이스를 .NET Framework 4.6부터 구현합니다. 이전 버전의 .NET Framework에서는 ConcurrentDictionary<TKey,TValue> 클래스가 이러한 인터페이스를 구현하지 않았습니다.
클래스 System.Collections.Generic.Dictionary<TKey,TValue> 와 ConcurrentDictionary<TKey,TValue> 마찬가지로 인터페이스를 IDictionary<TKey,TValue> 구현합니다. 또한 ConcurrentDictionary<TKey,TValue> 다음 표에 설명된 대로 사전에서 키/값 쌍을 추가하거나 업데이트하는 몇 가지 메서드를 제공합니다.
| 작업 | 이 메서드 사용 | 사용 현황 정보 |
|---|---|---|
| 사전에 새 키가 없는 경우 사전에 새 키를 추가합니다. | TryAdd | 이 메서드는 키가 사전에 현재 존재하지 않는 경우 지정된 키/값 쌍을 추가합니다. 메서드가 반환되거나 false 새 쌍이 추가되었는지 여부에 따라 달라집니다true. |
| 해당 키에 특정 값이 있는 경우 사전에서 기존 키의 값을 업데이트합니다. | 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> 수정 및 쓰기 작업의 경우 세분화된 잠금을 사용하여 스레드 보안을 보장합니다. (사전에 대한 읽기 작업은 잠금 없는 방식으로 수행됩니다.) 그러나 이러한 메서드에 대한 대리자는 잠금에서 알 수 없는 코드를 실행하여 발생할 수 있는 문제를 방지하기 위해 잠금 외부에서 호출됩니다. 따라서 이러한 대리자가 실행하는 코드에는 작업의 원자성이 적용되지 않습니다.
생성자
속성
| Name | Description |
|---|---|
| Count |
에 포함된 ConcurrentDictionary<TKey,TValue>키/값 쌍의 수를 가져옵니다. |
| IsEmpty |
비어 있는지 여부를 ConcurrentDictionary<TKey,TValue> 나타내는 값을 가져옵니다. |
| Item[TKey] |
지정된 키와 연결된 값을 가져오거나 설정합니다. |
| Keys |
에 있는 키가 Dictionary<TKey,TValue>포함된 컬렉션을 가져옵니다. |
| Values |
에 있는 값이 들어 있는 Dictionary<TKey,TValue>컬렉션을 가져옵니다. |
메서드
명시적 인터페이스 구현
확장명 메서드
적용 대상
스레드 보안
모든 공용 및 보호된 멤버 ConcurrentDictionary<TKey,TValue> 는 스레드로부터 안전하며 여러 스레드에서 동시에 사용할 수 있습니다. 그러나 확장 메서드를 ConcurrentDictionary<TKey,TValue> 포함하여 구현된 인터페이스 중 하나를 통해 액세스하는 멤버는 스레드로부터 안전하게 보호되지 않으며 호출자가 동기화해야 할 수도 있습니다.