ConcurrentDictionary<TKey, TValue> 类

【表示可由多个线程同时访问的键/值对的线程安全集合。】

支持 .NET Framework 4.0 及以上。

示例代码:

  1. class CD_Ctor
  2. {
  3. // Demonstrates:
  4. // ConcurrentDictionary<TKey, TValue> ctor(concurrencyLevel, initialCapacity)
  5. // ConcurrentDictionary<TKey, TValue>[TKey]
  6. static void Main()
  7. {
  8. // We know how many items we want to insert into the ConcurrentDictionary.
  9. // So set the initial capacity to some prime number above that, to ensure that
  10. // the ConcurrentDictionary does not need to be resized while initializing it.
  11. int NUMITEMS = ;
  12. int initialCapacity = ;
  13.  
  14. // The higher the concurrencyLevel, the higher the theoretical number of operations
  15. // that could be performed concurrently on the ConcurrentDictionary. However, global
  16. // operations like resizing the dictionary take longer as the concurrencyLevel rises.
  17. // For the purposes of this example, we'll compromise at numCores * 2.
  18. int numProcs = Environment.ProcessorCount;
  19. int concurrencyLevel = numProcs * ;
  20.  
  21. // Construct the dictionary with the desired concurrencyLevel and initialCapacity
  22. ConcurrentDictionary<int, int> cd = new ConcurrentDictionary<int, int>(concurrencyLevel, initialCapacity);
  23.  
  24. // Initialize the dictionary
  25. for (int i = ; i < NUMITEMS; i++) cd[i] = i * i;
  26.  
  27. Console.WriteLine("The square of 23 is {0} (should be {1})", cd[], * );
  28. }
  29. }

谢谢浏览!

线程安全集合 ConcurrentDictionary<TKey, TValue> 类的更多相关文章

  1. C#字典 Dictionary<Tkey,Tvalue> 之线程安全问题 ConcurrentDictionary<Tkey,Tvalue> 多线程字典

    ConcurrentDictionary<Tkey,Tvalue>  Model #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutra ...

  2. SortedDictionary<TKey, TValue> 类 表示根据键进行排序的键/值对的集合。

    SortedDictionary<TKey, TValue> 类   表示根据键进行排序的键/值对的集合. SortedDictionary<TKey, TValue> 中的每 ...

  3. “线程安全的” Dictionary(TKey,TValue)

    这是一篇翻译,专门介绍Dictionary线程安全问题,原文网址如下 http://www.grumpydev.com/2010/02/25/thread-safe-dictionarytkeytva ...

  4. Dictionary<TKey, TValue> 类

    C# Dictionary<TKey, TValue> 类 Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射.字典中的每个添加项都由一个值及 ...

  5. .net源码分析 - ConcurrentDictionary<TKey, TValue>

    List源码分析 Dictionary源码分析 ConcurrentDictionary源码分析 继上篇Dictionary源码分析,上篇讲过的在这里不会再重复 ConcurrentDictionar ...

  6. ConcurrentDictionary<TKey, TValue>的AddOrUpdate方法

    https://msdn.microsoft.com/zh-cn/library/ee378665(v=vs.110).aspx 此方法有一共有2个,现在只讨论其中一个 public TValue A ...

  7. 都说ConcurrentDictionary<TKey, TValue>有陷阱

    看这么几句解释(英文原帖): private static void ConcurrentDictionary() { var dict = new ConcurrentDictionary<i ...

  8. c# 扩展方法奇思妙用基础篇五:Dictionary<TKey, TValue> 扩展

    Dictionary<TKey, TValue>类是常用的一个基础类,但用起来有时确不是很方便.本文逐一讨论,并使用扩展方法解决. 向字典中添加键和值 添加键和值使用 Add 方法,但很多 ...

  9. .net学习笔记----有序集合SortedList、SortedList<TKey,TValue>、SortedDictionary<TKey,TValue>

    无论是常用的List<T>.Hashtable还是ListDictionary<TKey,TValue>,在保存值的时候都是无序的,而今天要介绍的集合类SortedList和S ...

随机推荐

  1. paip. 定时 关机 休眠 的总结

    paip. 定时 关机 休眠 的总结 //////////////////title bat shell 批处理日期时间的使用比较. bat shell 批处理if else 多行.. 作者Attil ...

  2. Hadoop-2.6.0 集群的 安装与配置

    1.  配置节点bonnie1 hadoop环境 (1) 下载hadoop- 2.6.0 并解压缩 [root@bonnie1 ~]# wget http://apache.fayea.com/had ...

  3. java方法创建

    一个方法public(作用域) void(void是不要返回值,String返回String类型,User(自定义的类型)返回User类型) test(方法名) (int a(参数)){ } stat ...

  4. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  5. mac系统安装php redis扩展

    安装步骤如下: 1.下载redis扩展 下载地址:https://nodeload.github.com/nicolasff/phpredis/zip/master 2.下载下来是zip包 手动解压 ...

  6. mysql 将时间戳直接转换成日期时间

    date为需要处理的参数(该参数是Unix 时间戳),可以是字段名,也可以直接是Unix 时间戳字符串 后面的 '%Y%m%d' 主要是将返回值格式化 例如: mysql>SELECT FROM ...

  7. curl上传、下载、https登陆

    1.下载 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "ftp://127.0.0.1/downtest.txt"); cur ...

  8. [推荐]Hadoop+HBase+Zookeeper集群的配置

    [推荐]Hadoop+HBase+Zookeeper集群的配置 Hadoop+HBase+Zookeeper集群的配置  http://wenku.baidu.com/view/991258e881c ...

  9. django开发的社区和博客

    社区 线上地址:http://codetheme.sinaapp.com/ Githubhttps://github.com/BeginMan/codetheme 由于利用两周下班时间熬夜做的,难免有 ...

  10. GTD时间管理(3)---项目

    一:什么是项目? 一个项目是由多步骤,多阶段组成的,不可能一步到位的. 项目分为可大可小. 魔兽世界这个程序是一个项目,是一个用10年开发的大型项目 搭建一个博客也可以成为一个项目,可以用一天时间去搭 ...