实现:

c#实现线程安全的List,主要还是给夹锁。代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Text;namespace Concurrent
{/// <summary>////// <typeparam name="T"></typeparam>public class ConcurrentList<T> : IList<T>, ICollection<T>, IEnumerable<T>{List<T> _list;// 摘要: //     初始化 System.Collections.Generic.List<T> 类的新实例,该实例为空并且具有默认初始容量。[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]public ConcurrentList(){_list = new List<T>();}//// 摘要: //     初始化 System.Collections.Generic.List<T> 类的新实例,该实例包含从指定集合复制的元素并且具有足够的容量来容纳所复制的元素。//// 参数: //   collection://     一个集合,其元素被复制到新列表中。//// 异常: //   System.ArgumentNullException://     collection 为 null。public ConcurrentList(IEnumerable<T> collection){_list = new List<T>(collection);}//// 摘要: //     初始化 System.Collections.Generic.List<T> 类的新实例,该实例为空并且具有指定的初始容量。//// 参数: //   capacity://     新列表最初可以存储的元素数。//// 异常: //   System.ArgumentOutOfRangeException://     capacity 小于 0。[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]public ConcurrentList(int capacity){_list = new List<T>(capacity);}public void CopyTo(T[] array, int index){lock (this)_list.CopyTo(array, index);}public int Count{get { return _list.Count; }}public void Clear(){lock (this) _list.Clear();}public bool Contains(T value){lock (this)return _list.Contains(value);}public int IndexOf(T value){lock (this)return _list.IndexOf(value);}public void Insert(int index, T value){lock (this)_list.Insert(index, value);}IEnumerator<T> IEnumerable<T>.GetEnumerator(){lock (this)return _list.GetEnumerator();}public void Add(T item){lock (this)_list.Add(item);}public bool Remove(T item){lock (this)return _list.Remove(item);}public void RemoveAt(int index){lock (this)_list.RemoveAt(index);}T IList<T>.this[int index]{get{lock (this)return _list[index];}set{lock (this)_list[index] = value;}}IEnumerator IEnumerable.GetEnumerator(){lock (this)return _list.GetEnumerator();}public bool IsReadOnly{get { return false; }}}
}

c# 实现线程安全的List容器相关推荐

  1. 并发编程-14线程安全策略之并发容器(J.U.C)中的集合类

    文章目录 J.U.C总览 脑图 概述 并发容器特性 示例 ArrayList对应的线程安全的并发容器类CopyOnWriteArrayList (线程安全) HashSet对应的线程安全的并发容器类C ...

  2. java 容器 线程_JAVA多线程并发容器

    1.ArrayList线程不安全:CopyOnWriteArrayList线程安全 package concurrent; import java.util.ArrayList; import jav ...

  3. C++技术之路:线程安全的map容器

    多线程环境下,我们如果用到map容器,我们就需要对map加锁.我们直接上代码,记录下我平时的代码. template< typename K, typename D > class CSt ...

  4. STL容器是否是线程安全的

    转载http://blog.csdn.net/zdl1016/article/details/5941330 STL的线程安全. 说一些关于stl容器的线程安全相关的话题. 一般说来,stl对于多线程 ...

  5. 并发编程-13线程安全策略之两种类型的同步容器

    文章目录 脑图 概述 同步容器 集合接口下的同步容器实现类 Vector (线程安全性比ArrayList好一些,但并非绝对线程安全) 同步容器 线程不安全的场景 其他注意事项 Hashtable C ...

  6. 6.切勿对STL容器的线程安全性有不切实际的依赖

    STL自身对多线程的支持非常有限.对于STL,你能期望的是: 多个线程读是安全的. 多个线程对不同的容器做写入操作时安全的. 在需要修改STL容器或这调用STL算法时需要自己加锁. 为了实现异常安全, ...

  7. Spring 中的bean 是线程安全的吗?

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 作者:myseries cnblogs.com/myser ...

  8. 验证ArrayList是线程不安全的集合

    package collectionSafe; import java.util.ArrayList;import java.util.Collections;import java.util.Lis ...

  9. Spring中的Controller ,Service,Dao是不是线程安全的?

    作者:myseries cnblogs.com/myseries/p/11729800.html 结论:不是线程安全的 Spring容器中的Bean是否线程安全,容器本身并没有提供Bean的线程安全策 ...

最新文章

  1. 打破认知:程序设计 #x3D; 算法 + 数据结构?
  2. 【数学与算法】【分段三次Hermite插值】和【分段三次样条插值】
  3. idea springboot 发布webservice 发布服务_太赞了:Spring boot+redis实现消息发布与订阅...
  4. spring入门案例plus
  5. jQuery Easy UI ProgressBar(进度条)组件
  6. 2dpca的matlab代码,2DPCA人脸识别的matlab代码
  7. 对象属性结构赋值_面向对象之构造器、代码块和内部类
  8. python面试代码题_常见python面试题-手写代码系列
  9. Ubuntu16.04源码安装postgresql-9.6.6数据库
  10. 等待线程结束(join)
  11. DataWorks调度配置最佳实战
  12. c++ find()
  13. 【勒索病毒数据恢复】Phobos勒索病毒家族之.[back23@vpn.tg].makop
  14. Guice依赖注入(接口多实现)
  15. python extractor_PyInstaller Extractor安装和使用方法
  16. linux怎么修改ftp虚拟用户账号密码,Linux下FTP虚拟账户配置
  17. android 儿童 汉字 学习 游戏,儿童学汉字游戏app
  18. strchr()函数的详解与实现
  19. ATECC508A芯片开发笔记(八):ECDH算法配置方法、执行过程及实现原理
  20. 如何压缩pdf文件的大小?四种方法值得收藏

热门文章

  1. 06_NetBean主类使用库项目类中的方法
  2. 数学建模----图与网络模型
  3. 毕业设计 Spring Boot的网上购物商城系统(含源码+论文)
  4. 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第53讲:PHP smarty模板配置及变量操作
  5. SonicWall防火墙IM禁止Skype软件
  6. 高斯投影正反算的代码
  7. 计算机发展简史的ppt教程,计算机发展简史教学文案.ppt
  8. 全球搜索引擎营销大会(上海站)即将召开
  9. 手把手教你读财报----银行业---第十课
  10. Mysql5.7.x下载安装及64位操作系统问题