static void Main(string[] args)
{
var concurrentDictionary = new ConcurrentDictionary<int, string>();
var dictionary = new Dictionary<int, string>(); var sw = new Stopwatch();
sw.Start(); for (int i = 0; i < 1000000; i++)
{
lock (dictionary)
{
dictionary[i] = Item;
}
} sw.Stop();
Console.WriteLine("wrinting to dictionary with a lock: {0}", sw.Elapsed);
//wrinting to dictionary with a lock: 00:00:00.0633939
sw.Restart();
for (int i = 0; i < 1000000; i++)
{
concurrentDictionary[i] = Item;
}
sw.Stop();
Console.WriteLine("wrinting to a concurrent dictionary: {0}", sw.Elapsed);
//wrinting to a concurrent dictionary: 00:00:00.2889851
//对于写入操作并发词典要比普通带锁词典要慢
sw.Restart();
for (int i = 0; i < 1000000; i++)
{
lock (dictionary)
{
CurrentItem = dictionary[i];
}
}
sw.Stop();
Console.WriteLine("reading from dictionary with a lock: {0}", sw.Elapsed);
//reading from dictionary with a lock: 00:00:00.0286066
sw.Restart();
for (int i = 0; i < 1000000; i++)
{
CurrentItem = concurrentDictionary[i];
}
sw.Stop();
Console.WriteLine("reading from a concurrent dictionary: {0}", sw.Elapsed);
//reading from a concurrent dictionary: 00:00:00.0196372
//对于读取操作并发词典要比普通带锁词典要快
//concurrentDictionary采用细粒度锁定[fine-grained locking]
//普通带锁dictionary采用粗粒度锁定[coarse-grained locking]
//在多核多线程的情况下concurrentDictionary将有更好的性能表现
sw.Restart(); Console.ReadKey();
} const string Item = "Dictionary item";
public static string CurrentItem;

  

ConcurrentDictionary,ConcurrentStack,ConcurrentQueue的更多相关文章

  1. 基础才是重中之重~ConcurrentDictionary让你的多线程代码更优美

    回到目录 ConcurrentDictionary是.net4.0推出的一套线程安全集合里的其中一个,和它一起被发行的还有ConcurrentStack,ConcurrentQueue等类型,它们的单 ...

  2. 使用ConcurrentDictionary替代Hashtable对多线程的对象缓存处理

    在之前一段时间里面,我的基类多数使用lock和Hashtable组合实现多线程内缓存的冲突处理,不过有时候使用这两个搭配并不尽如人意,偶尔还是出现了集合已经加入的异常,对代码做多方的处理后依然如故,最 ...

  3. ConcurrentDictionary与Dictionary 替换

    本文导读:ASP.NET中ConcurrentDictionary是.Net4 增加的,相对于Dictionary的线程安全的集合, ConcurrentDictionary可实现一个线程安全的集合, ...

  4. 转载 三、并行编程 - Task同步机制。TreadLocal类、Lock、Interlocked、Synchronization、ConcurrentQueue以及Barrier等

    随笔 - 353, 文章 - 1, 评论 - 5, 引用 - 0 三.并行编程 - Task同步机制.TreadLocal类.Lock.Interlocked.Synchronization.Conc ...

  5. 三、并行编程 - Task同步机制。TreadLocal类、Lock、Interlocked、Synchronization、ConcurrentQueue以及Barrier等

    在并行计算中,不可避免的会碰到多个任务共享变量,实例,集合.虽然task自带了两个方法:task.ContinueWith()和Task.Factory.ContinueWhenAll()来实现任务串 ...

  6. HashTable、Dictionary、ConcurrentDictionary三者区别

    转载自https://blog.csdn.net/yinghuolsx/article/details/72952857 1.HashTable HashTable表示键/值对的集合.在.NET Fr ...

  7. ConcurrentDictionary让你的多线程代码更优美

    ConcurrentDictionary是.net4.0推出的一套线程安全集合里的其中一个,和它一起被发行的还有ConcurrentStack,ConcurrentQueue等类型,它们的单线程版本( ...

  8. 论C#逼格手册

    水文.如何让自己的代码看起来,更有逼格? 要想让自己的代码,看起来更优雅,更有逼格,更高大上,就一定要写出晦涩难懂,而又简洁的代码来. 对于类自身的全局变量,一定要加this,对于基类的,一定要加ba ...

  9. C#学习笔记---如何提高代码逼格

    水文.如何让自己的代码看起来,更有逼格? 要想让自己的代码,看起来更优雅,更有逼格,更高大上,就一定要写出晦涩难懂,而又简洁的代码来. 对于类自身的全局变量,一定要加this,对于基类的,一定要加ba ...

随机推荐

  1. ubuntu笔记1-vim安装报错

    ubuntu安装vim的时候,报错提示:vim : 依赖: vim-common (= 2:7.3.429-2ubuntu2) 但是 2:7.3.429-2ubuntu2.1 正要被安装 说明既存的v ...

  2. Java对姓名, 手机号, 身份证号, 地址进行脱敏

    替换几位就用几个*号 一.姓名 1, 脱敏规则: 只显示第一个汉字,比如李某某置换为李**, 李某置换为李* private static String desensitizedName(String ...

  3. [技术博客]利用第三方框架react-native-swipeout实现左右滑动出现按钮

    在之前的开发中,为了实现用户不同手势操作能够对应不同的功能,我们考虑使用React-Native的API--PanResponder,实现识别用户的手势,实现不同的功能.但我们很快就发现,这样简单的实 ...

  4. Django实现自动发布(1数据模型)

    公司成立之初,业务量较小,一个程序包揽了所有的业务逻辑,此时服务器数量少,上线简单,基本开发-测试-上线都是由开发人员完成. 随着业务量逐渐上升,功能增多,代码量增大,而单一功能上线需要重新编译整个程 ...

  5. Java NIO 文件通道使用

    读取一个文件的内容,然后写入另外一个文件 public class NioTest4 { public static void main(String[] args) throws Exception ...

  6. spring-boot 知识集锦

    1.spring-boot项目在外部tomcat环境下部署 https://blog.csdn.net/james_wade63/article/details/51009423 https://bl ...

  7. fluid.io.load_inference_model 载入多个模型的时候会报错 -- [paddlepaddle]

    将多个模型部署到同一个服务时,会出现stack错误. 原因是program为全局. 改成这样,可以解决. solved by myself. for those who need it:use a n ...

  8. yandex 图片自动下载

    yandex 图片自动下载命令行程序 一个在 yandex 上搜索图片并下载到本地的 node cli 程序. 使用帮助: $0 <搜索关键词> [-t=超时(默认 1000)] [-r ...

  9. 创建Observable序列

    1. just()方法 该方法通过传入一个默认值来初始化 下面样例我们显示地标注出了observable的类型为Observable, 即指定了这个Observable所发出的事件携带的数据类型必须是 ...

  10. phpspreadsheet 中文文档(五)节约内存+PHPExcel迁移

    2019年10月11日14:03:31 节省内存 PhpSpreadsheet在工作表中平均每个单元格使用约1k,因此大型工作簿可以迅速用尽可用内存.单元缓存提供了一种机制,使PhpSpreadshe ...