Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between SynchronizedCollection<T> and the other concurrent collections?

How does SynchronizedCollection<T> and the concurrent collections in the System.Collections.Concurrent namespace differ from each other, apart from Concurrent Collections being a namespace and SynchronizedCollection<T> being a class?

SynchronizedCollection<T> and all of the classes in Concurrent Collections provide thread-safe collections. How do I decide when to use one over the other, and why?

like image 358
Batrickparry Avatar asked Jan 11 '11 07:01

Batrickparry


People also ask

What is the difference between Synchronised collection and concurrent collection?

The main reason for this slowness is locking; synchronized collections lock the whole collection e.g. whole Map or List while concurrent collection never locks the whole Map or List. They achieve thread safety by using advanced and sophisticated techniques like lock stripping.

How are concurrent collection classes different from normal collection classes?

Traditional collections are type of legacy collection in Java and are introduced before concurrent collections. While concurrent collections are introduced in JDK 1.5 i.e. are introduced after traditional collections.

What is the difference between synchronization and concurrency?

The word synchronization generally means sharing data between multiple processors or threads, while concurrency refers to a measure of– or the art of improving– how effectively an application allows multiple jobs required by that application (e.g. serving web page requests from a web server) to run simultaneously.

Is SynchronizedCollection thread-safe?

SynchronizedCollection is simply not thread-safe in this application. Use Concurrent Collections instead.


1 Answers

The SynchronizedCollection<T> class was introduced first in .NET 2.0 to provide a thread-safe collection class. It does this via locking so that you essentially have a List<T> where every access is wrapped in a lock statement.

The System.Collections.Concurrent namespace is much newer. It wasn't introduced until .NET 4.0 and it includes a substantially improved and more diverse set of choices. These classes no longer use locks to provide thread safety, which means they should scale better in a situation where multiple threads are accessing their data simultaneously. However, a class implementing the IList<T> interface is notably absent among these options.

So, if you're targeting version 4.0 of the .NET Framework, you should use one of the collections provided by the System.Collections.Concurrent namespace whenever possible. Just as with choosing between the various types of collections provided in the System.Collections.Generic namespace, you'll need to choose the one whose features and characteristics best fit your specific needs.

If you're targeting an older version of the .NET Framework or need a collection class that implements the IList<T> interface, you'll have to opt for the SynchronizedCollection<T> class.

This article on MSDN is also worth a read: When to Use a Thread-Safe Collection

like image 184
Cody Gray Avatar answered Sep 28 '22 11:09

Cody Gray