Exploring the ConcurrentDictionary Class in detail

ConcurrentDictionary is a type from .NET 4.0 and System.Collections.Concurrent namespace for handling multiple threads. It allows multiple threads to access a dictionary instance concurrently. This Type makes adding, removing and updating values in a lookup table on multiple threads easier. It's actually a thread safe version of Dictionary collection type that you don’t need to use synchronization. Most of the methods in ConcurrentDictionary are the same as Dictionary methods, but there are some extra methods that I'm gonna to explain them:

 

TryAdd(): this is an atomic operation to check if the item exist, if not add it. This is a replacement for add();

  1.         public void TryAddTest()
  2.         {
  3.             var concurrentDictionary = new ConcurrentDictionary<string, int>();
  4.             concurrentDictionary.TryAdd("Collection", 1);
  5.         }

 

 TryGetValue() :This is just like the TryGetValue() on the Dictionary.

  1.         public void TryGetValueTest()
  2.         {
  3.             var concurrentDictionary = new ConcurrentDictionary<string, int>();
  4.             int outVal;
  5.             if (concurrentDictionary.TryGetValue("Collection", out outVal))
  6.             {
  7.                 //Do something
  8.             }
  9.         }

 

TryRemove(): this is atomic safe remove a little bit different from remove method of Dictionary. It's second out parameter checks  if the key exist or not to remove it.

  1.         public void TryRemoveTest()
  2.         {
  3.             var concurrentDictionary = new ConcurrentDictionary<string, int>();
  4.             int removedValue;
  5.             if (concurrentDictionary.TryRemove("Collection", out removedValue))
  6.             {
  7.                 //success
  8.             }
  9.         }

 

TryUpdate(): This checks if the item is exist and then update the item in one atomic step.

  1.         public void TryUpdateTest()
  2.         {
  3.             var concurrentDictionary = new ConcurrentDictionary<string, int>();
  4.             concurrentDictionary.TryAdd("Machine", 1);
  5.             concurrentDictionary.TryAdd("bicycle", 2);
  6.  
  7.             concurrentDictionary.TryUpdate("Machine", 200, 4); //this does not do anything, because the value of Machine is not 1 !
  8.             concurrentDictionary.TryUpdate("bicycle", 100, 1); // this will work
  9.  
  10.         }

 

GetOrAdd(): make you sure that if an item exist in the cache. It's a thread safe get and insert.

  1.         public void GetOrAddTest()
  2.         {
  3.             var concurrentDictionary = new ConcurrentDictionary<string, int>();
  4.             concurrentDictionary.TryAdd("Machine", 1);
  5.             concurrentDictionary.TryAdd("bicycle", 2);
  6.  
  7.             // Get motorCycle or add it with value of 4.
  8.             int motorCycle = concurrentDictionary.GetOrAdd("MotorCycle", 4);
  9.  
  10.         }

 

 AddOrUpdate(): when you want to insert an item into dictionary or update the existing item. This will check if the item exist or not.

  1.         public void AddOrUpdateTest()
  2.         {
  3.             var concurrentDictionary = new ConcurrentDictionary<string, int>();
  4.             concurrentDictionary.TryAdd("Machine", 1);
  5.             concurrentDictionary.TryAdd("bicycle", 2);
  6.  
  7.             // Add Machine with value of 5 if it does NOT exist.
  8.             // ... Otherwise, add one to its value.
  9.             concurrentDictionary.AddOrUpdate("Machine", 5, (k, v) => v + 4);
  10.         }

 

 AddOrUpdate() and GetOrAdd() they are thread safe but they are not atomic

 

 

 

Resources:

  1. http://www.dotnetperls.com/concurrentdictionary
  2. http://geekswithblogs.net/BlackRabbitCoder/archive/2011/02/17/c.net-little-wonders-the-concurrentdictionary.aspx
  3. http://www.codeproject.com/Articles/548406/Dictionary-plus-Locking-versus-ConcurrentDictionar


Tags: C#


comments powered by Disqus