On Not "using System.Collections.Concurrent"

来源:互联网 发布:windows10自带看图软件 编辑:程序博客网 时间:2024/04/30 16:25
       先看看微软官方文档上的说明吧: "TheSystem.Collections.Concurrent namespace provides several thread-safe collection classes that should be used in place of the corresponding types in the System.Collections  and System.Collections.Generic namespaces whenever multiple threads are accessing the collection concurrently."

    以ConcurrentDictionary<(Of<(TKey, TValue>)>) 为例又有如下的说明:"ConcurrentDictionary <(Of<(TKey, TValue>)>)is designed for multithreaded scenarios. You do not have to use locks in your code to add or remove items from the collection. However, it is always possible for one thread to retrieve a value, and another thread to immediately update the collection by giving the same key a new value. "。

    "Note that ConcurrentDictionary is the one concurrent collection that does not support thread-safe enumeration"。到底还是要有自己写“lock”的时候。

    我想微软的初衷,应该是为了方便用户。我不知道在一些对性能要求很高的环境下,System.Collections.Concurrent下的集合其性能与自己写同步其差别有多大,但是在普通情况下,我觉得还是自己写同步比较好,这样比较放心。所以,我还是打算将这个域名束之高阁。

原创粉丝点击