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. iOS-触摸事件、手势识别、摇晃事件、耳机线控

    概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事件(手势操作).运动事件. ...

  2. NGUI ScrollView 循环 Item 实现性能优化

    今天来说说一直都让我在项目中头疼的其中一个问题,NGUI 的scrollView 列表性能问题,实现循环使用item减少性能上的开销. 希望能够给其他同学们使用和提供一个我个人的思路,这个写的不是太完 ...

  3. python多线程生成缩略图

    在img目录下7张图片 分别是 11.jpg 22.jpg 33.jpg 44.jpg 55.jpg 66.jpg 77.jpg #encoding=utf-8 import os import ti ...

  4. shell切割日志脚本

    #!/bin/bash set -e source /etc/bashrc cd `dirname $` linenum=`wc -l userinfolist.txt | awk '{print $ ...

  5. [推荐]PMO学习贴大集合

    [推荐]PMO学习贴大集合 http://wenku.baidu.com/view/a9b19bd4240c844769eaeed9.html http://wenku.baidu.com/view/ ...

  6. [推荐]DDOS攻击与防范知识介绍

    [推荐]DDOS攻击与防范知识介绍 DDOS攻防体系建设v0.2(淘宝-林晓曦)     http://wenku.baidu.com/view/39549a11a8114431b90dd866.ht ...

  7. Accessorizer的使用说明!

    工欲善其事,必先利其器 还是这句老话!但是老话藏真金! 今天我们来说说Accessorizer这个软件吧!!Accessorizer这个软件真的很不错!!但是我没有见到程序员使用它!! Accesso ...

  8. fidder 抓 https包配置方法(ios & android & pc浏览器)

    1. fidder抓https包的基本配置,可参见以下博文 http://blog.csdn.net/idlear/article/details/50999490 2. 遇到问题:抓包看只有Tunn ...

  9. [Aaronyang]谈谈2015年AY对WPF全面技术总结40多篇WPF,炫到没朋友的AYUI来了

             原著:AY WPF博客- 把wpf推广出去,让那些鄙视的人说不 大家好! 我是AY,首先声明,我在做一件很枯燥的事情,我是个91后程序员,每天熬夜完成计划的过着下班后的生活. 那天有 ...

  10. 如何清除朗逸保养提示标志INSP

    自己消除insp小扳手方法 具体步骤如下1.插入钥匙,不要转动.2.按住显示屏下方右边的黑圆柱按钮 3.钥匙转到2档,通电自检,期间按住按钮不要松手4.过10秒左右,INSP消失,小扳手自己去除 不用 ...