Thread Safe Concurrent Collection in C#

Introduction

The .NET framework offers some collection classes specifically used in multithreading. These collections are internally used synchronization hence we can call them thread safe collections. These collections can be accessed by multiple threads at a time hence they are called concurrent collections.

Here is the list of concurrent collections in C# 4.0

  • ConcurrentStack<T>
  • ConcurrentQueue<T>
  • BlockingCollection<T>
  • ConcurrentBag<T>
  • ConcurrentDictionary<TKey,T>

Let’s discuss ConcurrentCollections in detail,

ConcurrentStack<T>: This is thread-safe last-in-first-out collection.

Difference between Stack and Concurrent Stack

Sr. No

Stack<T>

ConcurrentStack<T>

1

Not thread-safe

Thread-safe

2

Stack has Pop method

ConcurrentStaick has TryPop method

3

Can add single item at a time.

Can add multiple item at a time.

4

Can remove single item at a time

Can remove multiple item at a time.

5

Add item using Push method

Add item using or PushRange method

6

Remove item using Pop method

Remove item using TryPop or TryPopRange method.

Push: Add item at top of ConcurrentStack.

PushRange: Tries to add multiple items at top of ConcurrentStack.

TryPeek: Tries to return last item from ConcurrentStack.

TryPop: Tries to pop and return last item from ConcurrentStack.

ConcurrentQueue<T>: This is thread-safe first-in-first-out collection.

Difference between Queue and ConcurrentQueue

Sr. No

Queue<T>

ConcurrentQueue<T>

1

Not thread-safe

Thread-safe

2

Queue has Enqueue and Dequeue methods

ConcurrentQueue has Enqueue and TryDequeue methods

3

Can add single item at a time.

Can add multiple item at a time.

4

Can remove single item at a time

Can remove multiple item at a time.

Enqueue: Add item to end of ConcurrentQueue.

TryDequeue: Tries to remove and return first item from ConcurrentQueue.

TryPeek: Return first item from ConcurrentQueue.

BlockingCollection<T> 

This collection is thread-safe for adding and removing data. You can set maximum upper limit to add items in this collection. Removing an item from     the collection can be blocked until item becomes available. 

Add: Add item into BlockingCollection.

Take: Removes item from BlockingCollection .

TryAdd: Tries to add item into BlockingCollection.

TryTake: Tries to remove item from BlockingCollection.

ConcurrentBag<T>

ConcurrentBag is thread-safe collection. This collection is like just at bag or heap of objects. It has not ordering and it allow duplicate items.

ConCurrentBag has mainly three methods.

Add: Add item into ConcurrentBag collection.

TryTake: Tries to remove and return item from ConcurrentBag.

TryPeek: Tries to return item from ConcurrentBag.

ConcurrentDictionary<TKey,T>

Concurrent dictionary is thread-safe collection. It is used to store key-value pair in multi-threaded environment.

Add: Add item into concurrent dictionary.

Remove: Remove item from concurrent dictionary.

Update: Update existing item in concurrent dictionary.

TryUpdate: This method check whether item is exist or not before update.

AddOrUpdate: This method add item into concurrent dictionary if that object does not exist.

GetOrAdd: This method return current value in dictionary if it exist otherwise add as new value.

Read more articles on Multithreading:

Up Next
    Ebook Download
    View all
    Learn
    View all