The System.Collections.Concurrent namespace includes several collection classes that are both thread-safe and scalable. Multiple threads can safely and efficiently add or remove items from these collections, without requiring additional synchronization in user code. When you write new code, use the concurrent collection classes to write multiple threads to the collection concurrently. If you're only reading from a shared collection, then you can use the classes in the System.Collections.Generic namespace.
System.Collections and System.Collections.Generic
The collection classes in the System.Collections namespace include ArrayList and Hashtable. These classes provide some thread safety through the
Synchronized property, which returns a thread-safe wrapper around the collection. The wrapper works by locking the entire collection on every add or remove operation. Therefore, each thread that's attempting to access the collection must wait for its turn to take the one lock. This process isn't scalable and can cause significant performance degradation for large collections. Also, the design isn't protected from race conditions. For more information, see Synchronization in Generic Collections.
The collection classes in the System.Collections.Generic namespace include List<T> and Dictionary<TKey,TValue>. These classes provide improved type safety and performance compared to the System.Collections classes. However, the System.Collections.Generic classes don't provide any thread synchronization; user code must provide all synchronization when items are added or removed on multiple threads concurrently.
We recommend using the concurrent collections classes in the System.Collections.Concurrent namespace because they provide type safety and also more efficient and complete thread safety.
Fine-grained locking and lock-free mechanisms
Some of the concurrent collection types use lightweight synchronization mechanisms such as SpinLock, SpinWait, SemaphoreSlim, and CountdownEvent. These synchronization types typically use busy spinning for brief periods before they put the thread into a true
Wait state. When wait times are expected to be short, spinning is far less computationally expensive than waiting, which involves an expensive kernel transition. For collection classes that use spinning, this efficiency means that multiple threads can add and remove items at a high rate. For more information about spinning versus blocking, see SpinLock and SpinWait.
The ConcurrentQueue<T> and ConcurrentStack<T> classes don't use locks at all. Instead, they rely on Interlocked operations to achieve thread safety.
Because the concurrent collections classes support ICollection, they provide implementations for the IsSynchronized and SyncRoot properties, even though these properties are irrelevant.
IsSynchronized always returns
SyncRoot is always
Nothing in Visual Basic).
The following table lists the collection types in the System.Collections.Concurrent namespace:
|BlockingCollection<T>||Provides bounding and blocking functionality for any type that implements IProducerConsumerCollection<T>. For more information, see BlockingCollection Overview.|
|ConcurrentDictionary<TKey,TValue>||Thread-safe implementation of a dictionary of key-value pairs.|
|ConcurrentQueue<T>||Thread-safe implementation of a FIFO (first-in, first-out) queue.|
|ConcurrentStack<T>||Thread-safe implementation of a LIFO (last-in, first-out) stack.|
|ConcurrentBag<T>||Thread-safe implementation of an unordered collection of elements.|
|IProducerConsumerCollection<T>||The interface that a type must implement to be used in a
|BlockingCollection Overview||Describes the functionality provided by the BlockingCollection<T> type.|
|How to: Add and Remove Items from a ConcurrentDictionary||Describes how to add and remove elements from a ConcurrentDictionary<TKey,TValue>|
|How to: Add and Take Items Individually from a BlockingCollection||Describes how to add and retrieve items from a blocking collection without using the read-only enumerator.|
|How to: Add Bounding and Blocking Functionality to a Collection||Describes how to use any collection class as the underlying storage mechanism for an IProducerConsumerCollection<T> collection.|
|How to: Use ForEach to Remove Items in a BlockingCollection||Describes how to use
|How to: Use Arrays of Blocking Collections in a Pipeline||Describes how to use multiple blocking collections at the same time to implement a pipeline.|
|How to: Create an Object Pool by Using a ConcurrentBag||Shows how to use a concurrent bag to improve performance in scenarios where you can reuse objects instead of continually creating new ones.|
Submit and view feedback for