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. 使用List中remove方法时需要注意的问题

    String str1 = new String("1"); String str2 = new String("2"); String str3 = new ...

  2. MySQL索引失效的几种场景

    我们都知道建立索引能够提高查询效率,那么是不是任何情况下都能提高呢,当然不是的的,下面我们就来列举一些常见的索引失效的场景. 借用上一篇文章的dm_person_info表 在card_code列没加 ...

  3. 强烈建议为你的Android项目加上 largeHeap 属性

    小内存机器使用“微信”时,看视频经常崩溃(out of memory) ,小内存机器有时候明明内存还很多,却还是抛出“内存不够”,应该就是每个APP能用“堆大小”的限制. 如上图,Android项目的 ...

  4. HTML5中使用EventSource实现服务器发送事件

    在HTML5的服务器发送事件中,使用EventSource对象可以接收服务器发送事件的通知. 示例: es.html <!DOCTYPE html> <html> <he ...

  5. Navicat 12.x for MySQL最新版安装破解教程(附安装包和注册机,全网独家可用

    title: "Navicat 12.x for MySQL最新版安装破解教程(附安装包和注册机,全网独家可用" categories: soft tags: soft autho ...

  6. 异常值检验实战3_NBA球员表现稳定性分析

     机器学习_深度学习_入门经典(博主永久免费教学视频系列) https://study.163.com/course/courseMain.htm?courseId=1006390023&sh ...

  7. 码云push时提示 DeployKey does not support push code fatal: Could not read from remote repository.

    一.如果需要push代码到码云,需要创建个人公钥,公共公钥只可以读不可以修改 二.执行代码即可:git push

  8. Linux的.a、.so和.o文件 对比 window下的dll,lib,exe文件

    连续几天终于将一个又一个问题解决了,这里说其中一个问题 描述问题:使用多线程pthread的时候,(我用的IDE,CODEBOLCKS)编译后发现直接弹出窗口,程序还没有被Build..巴拉巴拉,然后 ...

  9. TCP报文结构

    来自:https://blog.csdn.net/qq_32998153/article/details/79680704

  10. php的工厂模式

    特点 :将调用者和创建者分离,调用者直接向工厂类请求获取调用对象,减少代码耦合,提高系统的维护性和扩展性. <?php // **** 共同接口 **** // interface DB { f ...