Using concurrent collections for parallelism
Collections classes are one of the most commonly used types to encapsulate, retrieve, and modify enumerated sets of related data. Dictionary, list, queue, and array are some of the frequently used collection types, but they are not thread-safe. These collections are good if you access them from just one thread at a time. A real-world environment would be multithreaded, and to make it thread-safe, you will have to implement various synchronization constructs as described in an earlier section. To solve this problem, Microsoft came up with concurrent collection classes, such as ConcurrentQueue, ConcurrentBag, ConcurrentDictionary, ConcurrentStack, and so on, which are thread-safe as they internally implement synchronization. Let's look at them in detail in the following sections.
ConcurrentDictionary
Let's stimulate a multi-threaded environment using a dictionary. Consider task t1 as one operation from a client who is adding...