C#字典 Dictionary<Tkey,Tvalue> 之线程安全问题 ConcurrentDictionary<Tkey,Tvalue> 多线程字典
ConcurrentDictionary<Tkey,Tvalue> Model
#region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll
#endregion using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices; namespace System.Collections.Concurrent
{
//
// 摘要:
// 表示可由多个线程同时访问的键值对的线程安全集合。
//
// 类型参数:
// TKey:
// 字典中的键的类型。
//
// TValue:
// 字典中的值的类型。
[ComVisible(false)]
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(Generic.Mscorlib_DictionaryDebugView<,>))]
[DefaultMember("Item")]
public class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable
{
//
// 摘要:
// 初始化 System.Collections.Concurrent.ConcurrentDictionary`2 类的新实例,该实例为空,具有默认的并发级别和默认的初始容量,并为键类型使用默认比较器。
public ConcurrentDictionary();
//
// 摘要:
// 初始化 System.Collections.Concurrent.ConcurrentDictionary`2 类的新实例,该实例包含从指定的 System.Collections.IEnumerable{KeyValuePair{TKey,TValue}}
// 中复制的元素,具有默认的并发级别和默认的初始容量,并为键类型使用默认比较器。
//
// 参数:
// collection:
// System.Collections.IEnumerable{KeyValuePair{TKey,TValue}},其中的元素将要复制到新的 System.Collections.Concurrent.ConcurrentDictionary`2
// 中。
//
// 异常:
// T:System.ArgumentNullException:
// collection 是 null 引用(在 Visual Basic 中为 Nothing)。
//
// T:System.ArgumentException:
// collection 包含一个或多个重复键。
public ConcurrentDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection);
//
// 摘要:
// 初始化 System.Collections.Concurrent.ConcurrentDictionary`2 类的新实例,该实例为空,具有默认的并发级别和容量,并使用指定的
// System.Collections.Generic.IEqualityComparer{TKey}。
//
// 参数:
// comparer:
// 在比较键时要使用的 System.Collections.Generic.IEqualityComparer{TKey} 实现。
//
// 异常:
// T:System.ArgumentNullException:
// comparer 是 null 引用(在 Visual Basic 中为 Nothing)。
public ConcurrentDictionary(IEqualityComparer<TKey> comparer);
//
// 摘要:
// 初始化 System.Collections.Concurrent.ConcurrentDictionary`2 类的新实例,该实例为空,具有指定的并发级别和初始容量,并为键类型使用默认比较器。
//
// 参数:
// concurrencyLevel:
// 将同时更新 System.Collections.Concurrent.ConcurrentDictionary`2 的线程的估计数量。
//
// capacity:
// System.Collections.Concurrent.ConcurrentDictionary`2 可包含的初始元素数。
//
// 异常:
// T:System.ArgumentOutOfRangeException:
// concurrencyLevel 小于 1。- 或 -capacity 小于 0。
public ConcurrentDictionary(int concurrencyLevel, int capacity);
//
// 摘要:
// 初始化 System.Collections.Concurrent.ConcurrentDictionary`2 类的新实例,该实例包含从指定的 System.Collections.IEnumerable
// 中复制的元素,具有默认的并发级别和默认的初始容量,并使用指定的 System.Collections.Generic.IEqualityComparer{TKey}。
//
// 参数:
// collection:
// System.Collections.IEnumerable{KeyValuePair{TKey,TValue}},其中的元素将要复制到新的 System.Collections.Concurrent.ConcurrentDictionary`2
// 中。
//
// comparer:
// 在比较键时要使用的 System.Collections.Generic.IEqualityComparer{TKey} 实现。
//
// 异常:
// T:System.ArgumentNullException:
// collection 是 null 引用(在 Visual Basic 中为 Nothing)。- 或 - comparer 是 null 引用(在 Visual
// Basic 中为 Nothing)。
public ConcurrentDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer);
//
// 摘要:
// 初始化 System.Collections.Concurrent.ConcurrentDictionary`2 类的新实例,该实例包含从指定的 System.Collections.IEnumerable
// 中复制的元素,具有指定的并发级别和指定的初始容量,并使用指定的 System.Collections.Generic.IEqualityComparer{TKey}。
//
// 参数:
// concurrencyLevel:
// 将同时更新 System.Collections.Concurrent.ConcurrentDictionary`2 的线程的估计数量。
//
// collection:
// System.Collections.IEnumerable{KeyValuePair{TKey,TValue}},其中的元素将要复制到新的 System.Collections.Concurrent.ConcurrentDictionary`2
// 中。
//
// comparer:
// 在比较键时要使用的 System.Collections.Generic.IEqualityComparer{TKey} 实现。
//
// 异常:
// T:System.ArgumentNullException:
// collection 是 null 引用(在 Visual Basic 中为 Nothing)。 - 或 - comparer 是 null 引用(在 Visual
// Basic 中为 Nothing)。
//
// T:System.ArgumentOutOfRangeException:
// concurrencyLevel 小于 1。
//
// T:System.ArgumentException:
// collection 包含一个或多个重复键。
public ConcurrentDictionary(int concurrencyLevel, IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer);
//
// 摘要:
// 初始化 System.Collections.Concurrent.ConcurrentDictionary`2 类的新实例,该实例为空,具有指定的并发级别和指定的初始容量,并使用指定的
// System.Collections.Generic.IEqualityComparer{TKey}。
//
// 参数:
// concurrencyLevel:
// 将同时更新 System.Collections.Concurrent.ConcurrentDictionary`2 的线程的估计数量。
//
// capacity:
// System.Collections.Concurrent.ConcurrentDictionary`2 可包含的初始元素数。
//
// comparer:
// 在比较键时要使用的 System.Collections.Generic.IEqualityComparer{TKey} 实现。
//
// 异常:
// T:System.ArgumentNullException:
// comparer 是 null 引用(在 Visual Basic 中为 Nothing)。
//
// T:System.ArgumentOutOfRangeException:
// concurrencyLevel 小于 1。- 或 - capacity 小于 0。
public ConcurrentDictionary(int concurrencyLevel, int capacity, IEqualityComparer<TKey> comparer); //
// 摘要:
// 获取或设置与指定的键相关联的值。
//
// 参数:
// key:
// 要获取或设置的值的键。
//
// 返回结果:
// 返回指定索引处的 System.Collections.Generic.KeyValuePair`2 的 Value 属性。
//
// 异常:
// T:System.ArgumentNullException:
// key 是 null 引用(在 Visual Basic 中为 Nothing)。
//
// T:System.Collections.Generic.KeyNotFoundException:
// 已检索该属性,并且集合中不存在 key。
public TValue this[TKey key] { get; set; } //
// 摘要:
// 获取包含在 System.Collections.Concurrent.ConcurrentDictionary`2 中的键/值对的数目。
//
// 返回结果:
// 包含在 System.Collections.Concurrent.ConcurrentDictionary`2 中的键/值对的数目。
//
// 异常:
// T:System.OverflowException:
// 词典包含太多元素。
public int Count { get; }
//
// 摘要:
// 获取一个指示 System.Collections.Concurrent.ConcurrentDictionary`2 是否为空的值。
//
// 返回结果:
// 如果 System.Collections.Concurrent.ConcurrentDictionary`2 为空,则为 true;否则为 false。
public bool IsEmpty { get; }
//
// 摘要:
// 获取包含 System.Collections.Generic.Dictionary{TKey,TValue} 中的键的集合。
//
// 返回结果:
// 包含 System.Collections.Generic.Dictionary{TKey,TValue} 中的键的 System.Collections.Generic.ICollection{TKey}。
public ICollection<TKey> Keys { get; }
//
// 摘要:
// 获取包含 System.Collections.Generic.Dictionary{TKey,TValue} 中的值的集合。
//
// 返回结果:
// 一个 System.Collections.Generic.ICollection{TValue},它包含 System.Collections.Generic.Dictionary{TKey,TValue}
// 中的值。
public ICollection<TValue> Values { get; } //
// 摘要:
// 如果指定的键尚不存在,则将键/值对添加到 System.Collections.Concurrent.ConcurrentDictionary`2 中;如果指定的键已存在,则更新
// System.Collections.Concurrent.ConcurrentDictionary`2 中的键/值对。
//
// 参数:
// key:
// 要添加的键或应更新其值的键
//
// addValueFactory:
// 用于为空缺键生成值的函数
//
// updateValueFactory:
// 用于根据现有键的现有值为键生成新值的函数
//
// 返回结果:
// 键的新值。这将是 addValueFactory 的结果(如果缺少键)或 updateValueFactory 的结果(如果存在键)。
//
// 异常:
// T:System.ArgumentNullException:
// key 是 null 引用(在 Visual Basic 中为 Nothing)。- 或 -addValueFactory 是 null 引用(在 Visual
// Basic 中为 Nothing)。- 或 -updateValueFactory 是 null 引用(在 Visual Basic 中为 Nothing)。
//
// T:System.OverflowException:
// 词典包含太多元素。
public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory);
//
// 摘要:
// 如果指定的键尚不存在,则将键/值对添加到 System.Collections.Concurrent.ConcurrentDictionary`2 中;如果指定的键已存在,则更新
// System.Collections.Concurrent.ConcurrentDictionary`2 中的键/值对。
//
// 参数:
// key:
// 要添加的键或应更新其值的键
//
// addValue:
// 要为空缺键添加的值
//
// updateValueFactory:
// 用于根据现有键的现有值为键生成新值的函数
//
// 返回结果:
// 键的新值。这将是 addValue 的结果(如果缺少键)或 updateValueFactory 的结果(如果存在键)。
//
// 异常:
// T:System.ArgumentNullException:
// key 是 null 引用(在 Visual Basic 中为 Nothing)。- 或 -updateValueFactory 是 null 引用(在
// Visual Basic 中为 Nothing)。
//
// T:System.OverflowException:
// 词典包含太多元素。
public TValue AddOrUpdate(TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory);
//
// 摘要:
// 从 System.Collections.Concurrent.ConcurrentDictionary`2 中移除所有的键和值。
public void Clear();
//
// 摘要:
// 确定 System.Collections.Concurrent.ConcurrentDictionary`2 是否包含指定的键。
//
// 参数:
// key:
// 要在 System.Collections.Concurrent.ConcurrentDictionary`2 中查找的键。
//
// 返回结果:
// 如果 System.Collections.Concurrent.ConcurrentDictionary`2 包含具有指定键的元素,则为 true;否则为
// false。
//
// 异常:
// T:System.ArgumentNullException:
// key 是 null 引用(在 Visual Basic 中为 Nothing)。
public bool ContainsKey(TKey key);
//
// 摘要:
// 返回循环访问 System.Collections.Concurrent.ConcurrentDictionary`2 的枚举器。
//
// 返回结果:
// System.Collections.Concurrent.ConcurrentDictionary`2 的一个枚举器。
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator();
//
// 摘要:
// 如果指定的键尚不存在,则将键/值对添加到 System.Collections.Concurrent.ConcurrentDictionary`2 中。
//
// 参数:
// key:
// 要添加的元素的键。
//
// valueFactory:
// 用于为键生成值的函数
//
// 返回结果:
// 键的值。如果字典中已存在指定的键,则为该键的现有值;如果字典中不存在指定的键,则为 valueFactory 返回的键的新值。
//
// 异常:
// T:System.ArgumentNullException:
// key 是 null 引用(在 Visual Basic 中为 Nothing)。- 或 -valueFactory 是 null 引用(在 Visual
// Basic 中为 Nothing)。
//
// T:System.OverflowException:
// 词典包含太多元素。
public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory);
//
// 摘要:
// 如果指定的键尚不存在,则将键/值对添加到 System.Collections.Concurrent.ConcurrentDictionary`2 中。
//
// 参数:
// key:
// 要添加的元素的键。
//
// value:
// 指定的键不存在时要添加的值
//
// 返回结果:
// 键的值。如果字典中已存在指定的键,则为该键的现有值;如果字典中不存在指定的键,则为新值。
//
// 异常:
// T:System.ArgumentNullException:
// key 是 null 引用(在 Visual Basic 中为 Nothing)。
//
// T:System.OverflowException:
// 词典包含太多元素。
public TValue GetOrAdd(TKey key, TValue value);
//
// 摘要:
// 将 System.Collections.Concurrent.ConcurrentDictionary`2 中存储的键和值对复制到新数组中。
//
// 返回结果:
// 一个新数组,其中包含从 System.Collections.Concurrent.ConcurrentDictionary`2 复制的键和值对的快照。
public KeyValuePair<TKey, TValue>[] ToArray();
//
// 摘要:
// 尝试将指定的键和值添加到 System.Collections.Concurrent.ConcurrentDictionary`2 中。
//
// 参数:
// key:
// 要添加的元素的键。
//
// value:
// 要添加的元素的值。该值对于引用类型可以是空引用(在 Visual Basic 中为 Nothing)。
//
// 返回结果:
// 如果已将键/值对成功添加到 System.Collections.Concurrent.ConcurrentDictionary`2 中,则为 true;否则为
// false。
//
// 异常:
// T:System.ArgumentNullException:
// key 是 null 引用(在 Visual Basic 中为 Nothing)。
//
// T:System.OverflowException:
// System.Collections.Concurrent.ConcurrentDictionary`2 包含太多元素。
public bool TryAdd(TKey key, TValue value);
//
// 摘要:
// 尝试从 System.Collections.Concurrent.ConcurrentDictionary`2 获取与指定的键关联的值。
//
// 参数:
// key:
// 要获取的值的键。
//
// value:
// 此方法返回时,value 包含 System.Collections.Concurrent.ConcurrentDictionary`2 中具有指定键的对象;如果操作失败,则包含默认值。
//
// 返回结果:
// 如果在 System.Collections.Concurrent.ConcurrentDictionary`2 中找到该键,则为 true;否则为 false。
//
// 异常:
// T:System.ArgumentNullException:
// key 是 null 引用(在 Visual Basic 中为 Nothing)。
public bool TryGetValue(TKey key, out TValue value);
//
// 摘要:
// 尝试从 System.Collections.Concurrent.ConcurrentDictionary`2 中移除并返回具有指定键的值。
//
// 参数:
// key:
// 要移除并返回的元素的键。
//
// value:
// 此方法返回时,value 包含从 System.Collections.Concurrent.ConcurrentDictionary`2 中移除的对象;如果操作失败,则包含默认值。
//
// 返回结果:
// 如果成功移除了对象,则为 true;否则为 false。
//
// 异常:
// T:System.ArgumentNullException:
// key 是 null 引用(在 Visual Basic 中为 Nothing)。
public bool TryRemove(TKey key, out TValue value);
//
// 摘要:
// 将指定键的现有值与指定值进行比较,如果相等,则用第三个值更新该键。
//
// 参数:
// key:
// 其值将与 comparisonValue 进行比较并且可能被替换的键。
//
// newValue:
// 一个值,当比较结果相等时,将用该值替换具有 key 的元素的值。
//
// comparisonValue:
// 与具有 key 的元素的值进行比较的值。
//
// 返回结果:
// 如果 key 的值与 comparisonValue 相等并替换为 newValue,则为 true;否则为 false。
//
// 异常:
// T:System.ArgumentNullException:
// key 为 null 引用。
public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue);
}
}
//表示键和值的集合。
Dictionary<TKey, TValue>
C#字典 Dictionary<Tkey,Tvalue> 之线程安全问题 ConcurrentDictionary<Tkey,Tvalue> 多线程字典的更多相关文章
- java 多线程:线程安全问题,示例DateFormat多线程执行冲突解决方案ThreadLocal、方法内变量
SimpleDateFormat多线程中执行报错 java.lang.NumberFormatException: For input string: "" import ja ...
- 线程安全集合 ConcurrentDictionary<TKey, TValue> 类
ConcurrentDictionary<TKey, TValue> 类 [表示可由多个线程同时访问的键/值对的线程安全集合.] 支持 .NET Framework 4.0 及以上. 示例 ...
- javaweb回顾第六篇谈一谈Servlet线程安全问题
前言:前面说了很多关于Servlet的一些基础知识,这一篇主要说一下关于Servlet的线程安全问题. 1:多线程的Servlet模型 要想弄清Servlet线程安全我们必须先要明白Servlet实例 ...
- JavaWeb学习之Servlet(三)----Servlet的映射匹配问题、线程安全问题
[声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140529.html 一.Servlet映射匹配问题: 在第一篇文章中的 ...
- Parallel线程安全问题
废话不多说,上代码: using System; using System.Collections.Generic; using System.Threading.Tasks; namespace P ...
- (转)JavaWeb学习之Servlet(三)----Servlet的映射匹配问题、线程安全问题
[声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140529.html 一.Servlet映射匹配问题: 在第一篇文章中的 ...
- Servlet线程安全问题(转载)
转载地址:https://www.cnblogs.com/LipeiNet/p/5699944.html 前言:前面说了很多关于Servlet的一些基础知识,这一篇主要说一下关于Servlet的线程安 ...
- C#编程(五十三)----------字典Dictionary<TKey,TValue>
字典 关键字:Dicitionary 说明: 必须包含命名空间System.Collection.Generic Dictionary里面的每一个元素都是一个键值对(由两个元组组成:键和值). 键必须 ...
- C#中数组、集合(ArrayList)、泛型集合List<T>、字典(dictionary<TKey,TValue>)全面对比
C#中数组.集合(ArrayList).泛型集合List<T>.字典(dictionary<TKey,TValue>)全面对比 为什么把这4个东西放在一起来说,因为c#中的这4 ...
随机推荐
- LeetCode Crack Note --- 1. Two Sum
Discription Given an array of integers, return indices of the two numbers such that they add up to a ...
- iOS学习之Xcode 的Debug技巧
在Xcode中,Debug时,不能像eclipse ,或VS那些集成开发那样,能直接查看变量的值.那怎么在调试的时候查看XCode的变量呢? 有一些方法的. 1.新建一个Single View App ...
- JAVA heap space 内存溢出
3月28日开始使用JAVA调用SAP RFC后提示内存溢出 尝试增加set java_OPTS.....但仍然提示内存溢出 最终设置方法
- Slim安装以及使用【转】
最近在用backbone.js 做东西,因为牵扯到REST services 所以需要后台支持,此处选择了php.Slim 是php的一个框架. 貌似国内文章对此的介绍比较少,在安装Slim的过程中出 ...
- 06.Lucen入门程序-Field
需求: 实现一个歌词搜索系统,通过关键字搜索,凡是文件名或文件内容包括关键字的文件都要找出来. 注意:该入门程序只对文本文件(.txt)搜索. Lucene中包含两个重要的类: IndexWriter ...
- c#服务器端控件confirm
1>服务器端控件删除的数据的时候,可以调用js的confirm防止误操作,但是默认的样式实在是让人难以接受,怎么调用自定义的提示框而且同步回发到服务器端, 服务器端的控件的用linkbutton ...
- sqlserver 文件与文件组的使用和优化
文件和文件组填充策略 文件组对组内的所有文件都使用按比例填充策略.当数据写入文件组时,SQL Server 数据库引擎按文件中的可用空间比例将数据写入文件组中的每个文件,而不是将所有数据都写入 ...
- web.xml配置详解[转]
引文: 对于一个J2EE领域的程序员而言,基本上每天都会和web应用打交道. 什么是web应用?最简单的web应用什么样?给你一个web应用你该从何入手? 1.什么是web应用? web应用是一种可以 ...
- Mysql Join语法以及性能优化
引言 内外联结的区别是内联结将去除所有不符合条件的记录,而外联结则保留其中部分.外左联结与外右联结的区别在于如果用A左联结B则A中所有记录都会保留在结果中,此时B中只有符合联结条件的记录,而右联结相反 ...
- spark reduceByKey
reduce(binary_function) reduce将RDD中元素前两个传给输入函数,产生一个新的return值,新产生的return值与RDD中下一个元素(第三个元素)组成两个元素,再被传给 ...