zoukankan      html  css  js  c++  java
  • 线程安全集合 ConcurrentDictionary<TKey, TValue> 类

    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 = 64;
                int initialCapacity = 101;
    
                // 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 * 2;
    
                // 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 = 0; i < NUMITEMS; i++) cd[i] = i * i;
    
                Console.WriteLine("The square of 23 is {0} (should be {1})", cd[23], 23 * 23);
            }
    }

    谢谢浏览!

  • 相关阅读:
    图论分类讨论 bzoj2503相框
    高精+卡特兰数 bzoj3907网格
    树状数组 [Usaco2010 Nov]Cow Photographs
    二分图+贪心优化 [2009国家集训队]最大收益
    UINavigationItem表示UINavigationBar中的控件
    游历的路线
    2019.9.4 清点人数
    [国家集训队]矩阵乘法
    POJ 1113 Wall 凸包 裸
    POJ 1556 The Doors 线段交 dijkstra
  • 原文地址:https://www.cnblogs.com/Music/p/ConcurrentDictionary.html
Copyright © 2011-2022 走看看