ConcurrentDictionary<TKey, TValue> 类

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

支持 .NET Framework 4.0 及以上。

示例代码:

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

谢谢浏览!

线程安全集合 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. SQL Server 添加链接服务器

    背景 在SQL SERVER中,如果我们查询数据库需要关联另外一台数据库中表,在这种情况下我们可以通过添加服务器链接来实现. 案列 方式1.sql server 提供了图形化界面,如下: 右键> ...

  2. HTML5语义元素

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. This in JavaScript

    声明 本文仅为读书笔记并致力于理解Js中的this关键字.如有雷同,纯属巧合.原因有二,其一:有幸看了同一本书,其二:this这玩意已被说烂了~ 为什么要用this? this提供了优雅的方式隐式传递 ...

  4. python数据持久存储:pickle模块的使用

    python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...

  5. [ShareSDK for Android]新浪微博常见问题

    一.新浪sso授权报错sso package or sign error 1. 新浪微博开放平台应用没有审核通过,不能用sso登陆,否则报错.关闭sso登陆Platform platform = Sh ...

  6. xcode中嵌入framework(接入快用最新SDK遇到的问题)

      但xcode设置中并没有找到Embed Frameworks这个选项,使用以下方式添加 当遇到dyld: Library not loaded:xxxx  Reason: image not fo ...

  7. Android---真机调试时不能识别手机的解决方案

    我自己遇到的问题是VS2013和VS2015中xamarin无法识别到真机,现象就是插上手机开启调试模式,豌豆荚都可以连接上手机,就是VS上没出现手机. 但是低版本的手机貌似又行,4.0.3的. 于是 ...

  8. 开机SystemServer到ActivityManagerService启动过程分析

    开机SystemServer到ActivityManagerService启动过程 一 从Systemserver到AMS zygote-> systemserver:java入层口: /** ...

  9. GitHub前50名的Objective-C动画相关库

    GitHub的Objective-C的动画UI库其实是最多的一部分,GitHub有相当一部分的动画大牛,如Jonathan George,Nick Lockwood,Kevin,Roman Efimo ...

  10. COM是一个更好的C++

    昨天看了<COM本质论>的第一章”COM是一个更好的C++”,觉得很有必要做一些笔记,于是整理成这篇文章,我相信你值得拥有. 这篇文章主要讲的内容是:一个实现了快速查找功能的类FastSt ...