自定义Dictionary支持线程安全
本文转载:http://www.cnblogs.com/kiddo/archive/2008/09/25/1299089.html
public class SafeDictionary<TKey, TValue> : IDictionary<TKey, TValue>{private readonly object syncRoot = new object();private readonly Dictionary<TKey, TValue> d = new Dictionary<TKey, TValue>();#region IDictionary<TKey,TValue> Members/// <summary>/// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"></see>./// </summary>/// <param name="key">The object to use as the key of the element to add.</param>/// <param name="value">The object to use as the value of the element to add.</param>/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>/// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</exception>/// <exception cref="T:System.ArgumentNullException">key is null.</exception>public void Add(TKey key, TValue value){lock (syncRoot){d.Add(key, value);}}/// <summary>/// Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2"></see> contains an element with the specified key./// </summary>/// <param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</param>/// <returns>/// true if the <see cref="T:System.Collections.Generic.IDictionary`2"></see> contains an element with the key; otherwise, false./// </returns>/// <exception cref="T:System.ArgumentNullException">key is null.</exception>public bool ContainsKey(TKey key){return d.ContainsKey(key);}/// <summary>/// Gets an <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"></see>./// </summary>/// <value></value>/// <returns>An <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</returns>public ICollection<TKey> Keys{get{lock (syncRoot){return d.Keys;}}}/// <summary>/// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"></see>./// </summary>/// <param name="key">The key of the element to remove.</param>/// <returns>/// true if the element is successfully removed; otherwise, false. This method also returns false if key was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"></see>./// </returns>/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>/// <exception cref="T:System.ArgumentNullException">key is null.</exception>public bool Remove(TKey key){lock (syncRoot){return d.Remove(key);}}/// <summary>/// Tries the get value./// </summary>/// <param name="key">The key.</param>/// <param name="value">The value.</param>/// <returns></returns>public bool TryGetValue(TKey key, out TValue value){lock (syncRoot){return d.TryGetValue(key, out value);}}/// <summary>/// Gets an <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>./// </summary>/// <value></value>/// <returns>An <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</returns>public ICollection<TValue> Values{get{lock (syncRoot){return d.Values;}}}/// <summary>/// Gets or sets the <see cref="TValue"/> with the specified key./// </summary>/// <value></value>public TValue this[TKey key]{get { return d[key]; }set{lock (syncRoot){d[key] = value;}}}/// <summary>/// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"></see>./// </summary>/// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>public void Add(KeyValuePair<TKey, TValue> item){lock (syncRoot){((ICollection<KeyValuePair<TKey, TValue>>)d).Add(item);}}/// <summary>/// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"></see>./// </summary>/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only. </exception>public void Clear(){lock (syncRoot){d.Clear();}}/// <summary>/// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> contains a specific value./// </summary>/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>/// <returns>/// true if item is found in the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false./// </returns>public bool Contains(KeyValuePair<TKey, TValue> item){return ((ICollection<KeyValuePair<TKey, TValue>>)d).Contains(item);}/// <summary>/// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index./// </summary>/// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>/// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>/// <exception cref="T:System.ArgumentOutOfRangeException">arrayIndex is less than 0.</exception>/// <exception cref="T:System.ArgumentNullException">array is null.</exception>/// <exception cref="T:System.ArgumentException">array is multidimensional.-or-arrayIndex is equal to or greater than the length of array.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"></see> is greater than the available space from arrayIndex to the end of the destination array.-or-Type T cannot be cast automatically to the type of the destination array.</exception>public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex){lock (syncRoot){((ICollection<KeyValuePair<TKey, TValue>>)d).CopyTo(array, arrayIndex);}}/// <summary>/// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>./// </summary>/// <value></value>/// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</returns>public int Count{get { return d.Count; }}/// <summary>/// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only./// </summary>/// <value></value>/// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only; otherwise, false.</returns>public bool IsReadOnly{get { return false; }}/// <summary>/// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"></see>./// </summary>/// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>/// <returns>/// true if item was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false. This method also returns false if item is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"></see>./// </returns>/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>public bool Remove(KeyValuePair<TKey, TValue> item){lock (syncRoot){return ((ICollection<KeyValuePair<TKey, TValue>>)d).Remove(item);}}/// <summary>/// Returns an enumerator that iterates through the collection./// </summary>/// <returns>/// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection./// </returns>public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator(){return ((ICollection<KeyValuePair<TKey, TValue>>)d).GetEnumerator();}/// <summary>/// Returns an enumerator that iterates through a collection./// </summary>/// <returns>/// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection./// </returns>IEnumerator IEnumerable.GetEnumerator(){return ((IEnumerable)d).GetEnumerator();}#endregion}
ConcurrentDictionary<TKey, TValue> 类
详细可以查看:http://msdn.microsoft.com/zh-cn/library/dd287191(v=vs.110).aspx
线程安全集合类 | 非线程安全集合类 |
ConcurrentQueue<T> |
Queue<T> |
ConcurrentStack<T> |
Stack<T> |
ConcurrentBag<T> |
List<T> |
ConcurrentDictionary<TKey,TValue> |
Dictionary<TKey,TValue> |
转载于:https://www.cnblogs.com/51net/p/3963815.html
自定义Dictionary支持线程安全相关推荐
- 高并发编程-自定义简易的线程池(2),体会原理
文章目录 概述 示例 概述 高并发编程-自定义简易的线程池(1),体会原理 中只实现了任务队列,我们这里把其余的几个也补充进来 拒绝策略 关闭线程池 最小 最大 活动线程数 - 示例 比较简单,直接上 ...
- 不自定义异步方法的线程池默认使用SimpleAsyncTaskExecutor
如果不自定义异步方法的线程池默认使用SimpleAsyncTaskExecutor.SimpleAsyncTaskExecutor:不是真的线程池,这个类不重用线程,每次调用都会创建一个新的线程.并发 ...
- 自定义高效支持点击监听的RecyclerView
自定义高效支持点击监听的RecyclerView 效果图 Demo 地址:GitHub 传统做法 在Adapter内部直接对View添加点击事件 因为这种方式虽然也可以解决点击监听问题,但是效率不高, ...
- 自定义TextView支持第三方字体库(以隶书为例)
下载地址: http://download.csdn.net/download/jackwolf_gao/4973584 描述:自定义TextView支持第三方字体库(以隶书为例) 思路: 1,将字体 ...
- php全套之七,【独家首发】最新七星修改二开正米酷影视7.2完整版/支持自定义解析/支持PHP7.0及以上...
[独家首发]最新七星修改二开正米酷影视7.2完整版/支持自定义解析/支持PHP7.0及以上 视频播放页右侧栏图片和文字在后台→系统设置→APP设置修改广告位在后台设置~第一步:上传文件/uplod.z ...
- 主题 支持 php 7.2,最新七星修改二开正米酷影视7.2完整版/支持自定义解析/支持PHP7.0及以上...
[独家首发]最新七星修改二开正米酷影视7.2完整版/支持自定义解析/支持PHP7.0及以上 视频播放页右侧栏图片和文字在后台→系统设置→APP设置修改 广告位在后台设置~ 第一步:上传文件/uplod ...
- 咪咕代理php,【独家创业】新七星修改2开正咪咕影视7.2全版/支持自定义解析/支持PHP7.0及以上...
资料来源说明: [独家创业]新七星修改2开正咪咕影视7.2全版/支持自定义解析/支持PHP7.0及以上 视频播放页面右侧栏图文在后台→系统设置→APP设置更改 广告设置~在后台 步骤1:上传文件/up ...
- 小红书图片剪裁框架+微信图片选择器+超高清大图预览+图片自定义比例剪裁,支持 UI 自定义、支持跨进程回调
YImagePicker 项目地址:yangpeixing/YImagePicker 简介: 小红书图片剪裁框架+微信图片选择器+超高清大图预览+图片自定义比例剪裁,支持 UI 自定义.支持跨进程回调 ...
- Shiro第二篇【介绍Shiro、认证流程、自定义realm、自定义realm支持md5】
什么是Shiro shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. spring中有spring security (原名Acegi),是一个权限框架,它和sp ...
最新文章
- R语言编写自定义函数计算R方、使用自助法Bootstrapping估计多元回归模型的R方的置信区间、可视化获得的boot对象、估计单个统计量的置信区间、分别使用分位数法和BCa法
- 解决Linux中使用google chrome浏览器出现:ERR_PROXY_CONNECTION_FAILED 代理错误,导致不能够上网
- elasticsearch高亮显示查询结果
- Linux下的Cacti网络管理系统---安装(二)
- 初识Flink-从WorldCount开始
- Python Web初学解惑之 WSGI、flup、fastcgi、web.py的关系
- JavaScript学习笔记(八)--正则表达式
- iOS表示图下拉刷新控件
- 转:不同的行业和工作的真实情况是怎样的?
- 二值图像分析:轮廓形状逼近与拟合
- 戴尔电脑重装系统的blos设置
- 大厂面试Redis:缓存雪崩、缓存穿透、缓存击穿
- db(德邦快递单号查询)
- Cocos2dx之Scene和Scene Graph
- Python 绘制中国地图并标上国家名
- 弃用消息队列!新一代消息系统已成气候!
- 23种设计模式-桥梁模式《官渡之战》
- ar测量距离android,精度惊人!使用iPhone AR功能测量距离
- PDF怎么压缩大小,一分钟学会
- 在vue中使用 jquery分页组件
热门文章
- Python 调用shell脚本
- 琢磨琢磨,while (true) 和 for (;;) 哪个更快?!
- Spring Boot MongoDB 入门
- 如此火爆的ZooKeeper,到底如何选主?
- golang orm 框架之 gorm
- SpringBoot+Vue视频干货
- Android --- adapter.notifyDataSetChanged() 不起作用怎么办
- javafx 安装_JDK安装教程及环境配置
- 网传B站数据中心起火?所谓“现场图片”却是盗来的
- 两大电网大手笔投建能源大数据中心,15省都有哪些落地案例?