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. android常见错误之 No resource found that matches the given name

    新手上路,还希望大神多多照顾,刚自学android,遇到很多困难.其中就有这个问题,不知道你们遇到过没有,反正我是很头痛. No resource found that matches the giv ...

  2. Linux kill、kill-15、kill-9区别

    进程状态转换图 kill和kill -9,两个命令在linux中都有杀死进程的效果,然而两命令的执行过程却大有不同,在程序中如果用错了,可能会造成莫名其妙的现象. 执行kill(不加 -* 默认kil ...

  3. An overview of time series forecasting models

    An overview of time series forecasting models 2019-10-04 09:47:05 This blog is from: https://towards ...

  4. ubuntu18 任务栏调到底部

    $ gsettings set org.gnome.shell.extensions.dash-to-dock dock-position BOTTOM 原因如下: https://askubuntu ...

  5. 从ASP.NET到ASP.NET Core差异变化

    MSDN原文:链接 ASP.NET Core项目为开发人员提供了针对.NET Core,.NET Framework2种实现方式,根据官网通告NETCORE3.0后将取消对.NET Framework ...

  6. [转]C/C++实现回调机制的几种方式(回调、槽、代理)

    转自:https://www.jianshu.com/p/4f907bba6d5f (1)Callback方式(回调) Callback的本质是设置一个函数指针进去,然后在需要需要触发某个事件时调用该 ...

  7. 泡泡一分钟:Geometric and Physical Constraints for Drone-Based Head Plane Crowd Density Estimation

    张宁 Geometric and Physical Constraints for Drone-Based Head Plane Crowd Density Estimation 基于无人机的向下平面 ...

  8. git解决error: The following untracked working tree files would be overwritten by checkout

    在IDEA中进行分支切换时,出现如此错误,导致无法正常切换:error: The following untracked working tree files would be overwritten ...

  9. replace into 详解 update mysql

    转replace 与 update 区分本文主要对比一下 Sqlite 中的 replace 语句和 update 语句 . 在本例中使用如下数据库表:   图1 该表的表名为student, 存储学 ...

  10. 递归下降和LL(1)语法分析

    什么是自顶向下分析法 在语法分析过程中一般有两种语法分析方法,自顶向下和自底向上,递归下降分析和LL(1)都属于是自顶向下的语法分析 自顶向下分析法的过程就像从第一个非终结符作为根节点开始根据产生式进 ...