SynchronizedCollection< T>之间的区别是什么和其他并发集合? [英] What is the difference between SynchronizedCollection<T> and the other concurrent collections?

查看:164
本文介绍了SynchronizedCollection< T>之间的区别是什么和其他并发集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SynchronizedCollection< T> 如何以及 System.Collections.Concurrent 命名空间中的并发集合其他,除了并发集合是命名空间和 SynchronizedCollection< T> 是一个类吗?



SynchronizedCollection< T> ,并且并发集合中的所有类都提供线程安全集合。

解决方案

SynchronizedCollection< T> .NET 2.0提供了一个线程安全的集合类。它通过锁定这样做,所以你基本上有一个列表< T> 其中每个访问包裹在语句。



System.Collections.Concurrent 命名空间是更新的。它没有介绍,直到.NET 4.0和它包括一个显着改进和更多样化的选择集。这些类不再使用锁来提供线程安全,这意味着在多个线程同时访问它们的数据的情况下,它们应当更好地扩展。但是,实现 IList< T> 接口的类在这些选项中显然不存在。



为了针对4.0版的.NET Framework,你应该尽可能使用 System.Collections.Concurrent 命名空间提供的集合之一。正如在 系统中提供的各种类型的集合之间进行选择.Collections.Generic 命名空间,您需要选择其特性和特性最适合您的特定需求的。



如果您定位的是较旧版本的.NET Framework或需要一个实现 IList< T> 接口的集合类,则必须选择 SynchronizedCollection< T> class。



本文的MSDN也值得一读:何时使用线程安全收集


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?

解决方案

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

这篇关于SynchronizedCollection&lt; T&gt;之间的区别是什么和其他并发集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆