首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏技术/开源

    .net源码分析 - ConcurrentDictionary

    List源码分析 Dictionary源码分析 ConcurrentDictionary源码分析 继上篇Dictionary源码分析,上篇讲过的在这里不会再重复 ConcurrentDictionary github.com/dotnet/corefx/blob/master/src/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs 前言 ConcurrentDictionary一大特点是线程安全,在没有ConcurrentDictionary之前在多线程下用Dictionary,不管读写都要加个锁,不但麻烦,性能上也不是很好,因为在上篇分析中我们知道 下面看看ConcurrentDictionary里元素是做了怎样的封装。 总结 说完了,总结下,ConcurrentDictionary可以说是为了避免一个大锁锁住整个Dictionary带来的性能损失而出来的,当然也是采用空间换时间,不过这空间换得还是很值得的,一些object

    1.4K50发布于 2018-01-04
  • 来自专栏逸鹏说道

    ConcurrentDictionary 对决 Dictionary+Locking

    但现在,我们有了 ConcurrentDictionary。 这样看来,ConcurrentDictionary 扳回一局。 所以,在这种条件下 ConcurrentDictionary 又赢了一局。 这是 ConcurrentDictionary 的最强点。 在 ConcurrentDictionary 中,每个 Node 都是一个完整的类。

    1.8K70发布于 2018-04-10
  • 来自专栏科控自动化

    线程安全的字典ConcurrentDictionary

    但是,一旦学会了本节中的基础知识,就会发现 ConcurrentDictionary<TKey, TValue> 是非常实用的集合类型。 首先来看如何对集合写入值。 所有传入 ConcurrentDictionary<TKey, TValue> 的方法的委托,都同样遵循该原则。 讨论 虽然 ConcurrentDictionary<TKey, TValue> 是线程安全的,但这并不意味着它是原子操作。 当有多个线程读写共享集合时,最好使用 ConcurrentDictionary<TKey, TValue>。 ConcurrentDictionary<TKey, TValue> 最适用于共享数据的情况,在这种情况下,多个线程共享相同的集合。

    8.6K20编辑于 2022-03-29
  • 来自专栏dotNET编程大全

    C#中HashTable、Dictionary、ConcurrentDictionary区别

    {0}", item); } Console.ReadKey(); } } } 更多资料参考:Dictionary 类 三、ConcurrentDictionary ConcurrentDictionary<TKey, TValue> framework4出现的,可由多个线程同时访问,且线程安全。用法同Dictionary很多相同,但是多了一些方法。 ConcurrentDictionary 属于System.Collections.Concurrent 命名空间按照MSDN上所说: System.Collections.Concurrent 命名空间提供多个线程安全集合类 更多资料:ConcurrentDictionary<TKey,?TValue> 类 四、对比总结 分别插入500万条数据,然后遍历,看看耗时。 的1/10 单线程建议用Dictionary,多线程建议用ConcurrentDictionary或者HashTable(Hashtable tab = Hashtable.Synchronized(new

    1.1K20发布于 2021-11-01
  • 来自专栏精益码农

    ConcurrentDictionary字典操作竟然不全是线程安全的?

    标题不准确,实际上ConcurrentDictionary<TKey,TValue>绝大部分api都是线程安全且原子性的[1], 唯二的例外是接收工厂函数的api:AddOrUpdate、GetOrAdd <string, string> _dictionary = new ConcurrentDictionary<string, string>(); public static void <string, Lazy<string>> _lazyDictionary = new ConcurrentDictionary<string, Lazy<string>>(); • https://andrewlock.net/making-getoradd-on-concurrentdictionary-thread-safe-using-lazy/ ---- 总结 为解决ConcurrentDictionary /system.collections.concurrent.concurrentdictionary-2?

    84140编辑于 2022-12-21
  • 来自专栏林德熙的博客

    dotnet ConcurrentDictionary 的 GetOrAdd 性能比 TryGetValue 加 TryAdd 低

    } else { o = GetObject(); _concurrentDictionary.TryAdd for (int i = 0; i < Count; i++) { o = GetObject(); _concurrentDictionary.GetOrAdd object o = GetObject(); for (int i = 0; i < Count; i++) { o = _concurrentDictionary.GetOrAdd object o = null; for (int i = 0; i < Count; i++) { o = _concurrentDictionary.GetOrAdd (-1, _ => GetObject()); } return o; } 上面是测试 _concurrentDictionary 存在值的

    81130发布于 2020-07-28
  • 来自专栏林德熙的博客

    dotnet 警惕 ConcurrentDictionary 使用 FirstOrDefault 获取到非预期的首项

    在 dotnet 里面的 ConcurrentDictionary 是一个支持并发读写的线程安全字典,在这个字典里面有一些行为会出现随机性,即多次执行相同的代码返回的结果可能不相同。 本文记录在 ConcurrentDictionary 使用 FirstOrDefault 获取到非预期的首项的问题 在 dotnet 里面,无论是对 List 列表,还是 Dictionary 字典等获取首项 然而这个行为在 ConcurrentDictionary 里面是不成立的。 在 ConcurrentDictionary 里面如果使用 FirstOrDefault 方法,则随机获取到字典里面的一项,但对相同的一个 ConcurrentDictionary 对象多次调用 FirstOrDefault 方法,在不更改 ConcurrentDictionary 内容的情况下,可以稳定获取到相同的首项元素对象 简单来说就是在 ConcurrentDictionary 里面,调用 FirstOrDefault

    48910编辑于 2023-06-18
  • 来自专栏yaphetsfang

    ConcurrentDictionary线程不安全么,你难道没疑惑,你难道弄懂了么?

    前两篇写到关于断点传续的文章,还有一篇还未写出,后续会补上,这里我们穿插一篇文章,这是我看到同事写的代码中有ConcurrentDictionary这个类,之前并未接触过,就深入了解了一下,所以算是查漏补缺 private static readonly ConcurrentDictionary<string, string> _dictionary = new ConcurrentDictionary 为了不破坏原生调用ConcurrentDictionary的GetOrAdd方法,但是又为了保证线程安全,我们封装一个方法来方便进行调用。 { this.concurrentDictionary = new ConcurrentDictionary<TKey, Lazy<TValue>>(); 总结 本节我们学习了ConcurrentDictionary类里面有两个方法严格来说非线程安全,但是也可以得到相同的结果,若我们仅仅只是得到相同的结果且操作不是太耗时其实完全可以忽略这一点,若当利用ConcurrentDictionary

    94830发布于 2020-07-30
  • 来自专栏科控自动化

    [C#] 支持API的边缘网关开发笔记6-优化通讯池

    ConcurrentDictionary<string, IModbusMaster> connections = new ConcurrentDictionary<string, IModbusMaster public abstract void Write(writerequest<T> wreq); ///

    /// 通讯连接池 /// ConcurrentDictionary <string, IModbusMaster> connections = new ConcurrentDictionary<string, IModbusMaster>(); /// <summary namespace ModbusHandler { internal class Common { internal static var connections = new ConcurrentDictionary

    48010编辑于 2022-03-29
  • 来自专栏码农阿宇

    ConsurrentDictionary并发字典知多少?

    在研究ConcurrentDictionary的源码后,我觉得在ConcurrentDictionary的线程安全的解决思路很有意思,其对线程安全的处理对对我们项目中的其他高并发场景也有一定的参考价值, Concurrent ConcurrentDictionary是Dictionary的线程安全版本,位于System.Collections.Concurrent的命名空间下,该命名空间下除了有ConcurrentDictionary 的小伙伴,对这个ConcurrentDictionary的工作原理应该也不难理解,它是简简单单地在读写方法加个lock吗? ConcurrentDictionary ConcurrentDictionary的数据存储类似,只是buckets有个更多的职责,它除了有dictionary中的buckets的桥梁的作用外,负责了数据存储 由于官方原版的源码较为复杂,理解起来有所难度,我对官方源码做了一些精简,下文将围绕这个精简版的ConcurrentDictionary展开叙述.

    1.1K20发布于 2019-03-20
  • 来自专栏DotNet NB && CloudNative

    C# 如何处理和避免并发冲突?

    使用线程安全的集合类 .NET 提供了线程安全的集合类,如 ConcurrentDictionary、ConcurrentQueue 等。这些集合内置了并发控制,适合在高并发场景下使用。 示例代码: using System.Collections.Concurrent; var concurrentDictionary = new ConcurrentDictionary<int, string>(); Parallel.For(0, 10, i => { concurrentDictionary.TryAdd(i, $"Value{i}"); Console.WriteLine ($"Thread {Thread.CurrentThread.ManagedThreadId} added: {i}"); }); // 遍历集合 foreach (var item in concurrentDictionary ) { Console.WriteLine($"Key: {item.Key}, Value: {item.Value}"); } 说明: ConcurrentDictionary 自动处理线程间的数据同步

    70210编辑于 2024-12-23
  • 来自专栏magicodes

    2.Magicodes.NET框架之路——策略管理

    ///

    /// 策略字典 /// static private Lazy<ConcurrentDictionary<string, List<IStrategyBase >>> strategyDictionary = new Lazy<ConcurrentDictionary<string, List<IStrategyBase>>>(() => { return new ConcurrentDictionary<string, List<IStrategyBase>>(); }, LazyThreadSafetyMode.ExecutionAndPublication 为了使用方便,我们再定义一个属性来获取该值: public ConcurrentDictionary<string, List<IStrategyBase>> StrategyDictionary { ); /// /// 策略字典 /// public ConcurrentDictionary<string, List<IStrategyBase>

    59530发布于 2018-12-27
  • 来自专栏InCerry

    聊聊FASTER和进程内混合缓存

    C# FasterKV 性能测试 这是翻阅微软 Github 项目时,看到专门针对于 C#的 FasterKV 和 ConcurrentDictionary 的测试。 上图是 100%写入时的场景,随着线程数量的增加还在上涨,远超 ConcurrentDictionary,这和我们的测试结果相符合。 可以看到(Int32、Int64)类型确实让 ConcurrentDictionary 更快了,不过在有写入操作的场景,还是 FASTER 更胜一筹。 这也解释了一些我们上面的测试中,为什么 ConcurrentDictionary 在读场景那么快的原因之一,就是我们用了 Int64 作为 Key。 我的答案是可以,它虽然比不上纯内存的 ConcurrentDictionary,但是有着远超 RocksDB 等同类 KV Store 的性能。

    82320编辑于 2023-03-08
  • 来自专栏用户8907256的专栏

    C#中如何使用Parallel.For和Parallel.ForEach

    ConcurrentDictionary类包含在System.Collections.Concurrent命名空间内,并表示一个线程安全的字典。 以下两种方法都使用IsPrime方法检查整数是否为质数,将质数和托管线程ID存储在ConcurrentDictionary的实例中,然后返回该实例。第一种方法使用并发,第二种方法使用并行性。 private static ConcurrentDictionary<int, int> GetPrimeNumbersConcurrent(IList<int> numbers) { var primes = new ConcurrentDictionary<int, int>(); foreach (var number in numbers (IList<int> numbers) { var primes = new ConcurrentDictionary<int, int>();

    7.1K21发布于 2021-08-13
  • 来自专栏dotNET编程大全

    C# 看懂这100+行代码,你就真正入门了(经典)

    static CancellationTokenSource cancellationToken = new CancellationTokenSource(); private static ConcurrentDictionary <int, string> concurrentDictionary = new ConcurrentDictionary<int, string>(); private static 知识拓展:线程安全的队列和字典: ConcurrentDictionary<int, string> concurrentDictionary = new ConcurrentDictionary<

    85331发布于 2021-11-01
  • 来自专栏一路走一路失去也一路拥有

    C# Snet 采集库与转发库的最新应用示例代码,简单高效

    实际生产环境别瞎写喔,搞不好就出大事 //第一种写入 result = await operate.WriteAsync(new ConcurrentDictionary //第二种写入,更明确的写入,也是webapi请求的格式 result = await operate.WriteAsync(new ConcurrentDictionary GetSource<ConcurrentDictionary<string, AddressValue>>(); //直接输出结果 sender, EventDataResult e) { ConcurrentDictionary<string, AddressValue>? GetSource<ConcurrentDictionary<string, AddressValue>>(); //获取精简地址Protobuf集合对象

    32110编辑于 2025-03-18
  • 来自专栏walterlv - 吕毅的博客

    如何为非常不确定的行为(如并发)设计安全的 API,使用这些 API 时如何确保安全

    .NET 中提供了一些线程安全的类型,如 ConcurrentDictionary<TKey, TValue>,它们的 API 设计与常规设计差异很大。如果你对此觉得奇怪,那么正好阅读本文。 ---- 不确定性 像并发集合一样,如 ConcurrentDictionary<TKey, TValue>、ConcurrentQueue<T>,其设计为线程安全,于是它的每一个对外公开的方法调用都不会导致其内部状态错误 } 1 2 3 4 5 6 7 8 private ConcurrentDictionary<string, object> KeyValues { get; } = new ConcurrentDictionary > KeyValues { get; } = new ConcurrentDictionary<string, object>(); void Get(string key) { // ConcurrentDictionary 也正是考虑到了这种设计场景,于是才提供了 API GetOrAdd 方法。让你在获取对象实例的时候可以通过工厂方法去创建实例。

    1.7K20编辑于 2023-10-22
  • 来自专栏HueiFeng技术专栏

    ASP.NET WebAPI 测试文档 (Swagger)

    SwaggerTranslator.Translator(); 2.控制器描述和接口文档缓存 public class CachingSwaggerProvider : ISwaggerProvider { private static ConcurrentDictionary <string, SwaggerDocument> _cache = new ConcurrentDictionary<string, SwaggerDocument>(); private readonly srcDoc; } ///

    /// 从API文档中读取控制器描述 /// /// <returns>所有控制器描述</returns> public static ConcurrentDictionary string xmlpath = string.Format(@"{0}\App_Data\Api.xml", System.AppDomain.CurrentDomain.BaseDirectory); ConcurrentDictionary <string, string> controllerDescDict = new ConcurrentDictionary<string, string>(); if (File.Exists(xmlpath

    2.3K41发布于 2020-02-12
  • 来自专栏黄腾霄的博客

    2019-2-24-2019-2-24-C#中的弃元

    ---- 使用过ConcurrentDictionary<T,T>的同学肯定经历过这样的痛苦 var dict = new ConcurrentDictionary<int,int> dict[]=; 有了弃元之后,你就可以写出这样的代码 var dict = new ConcurrentDictionary<int,int>(); dict[1]=1; var result = dict.TryRemove

    60730发布于 2020-06-10
  • 来自专栏IT民工的代码世界

    ADO.NET事务封装

    在TransitionAction的DbTransaction添加的过程中,我们需要取到Task之外的ThreadID,这里称作为RootThreadID,同时维护一个ConcurrentDictionary TransitionAction合并到Root事物中 TransactionManage 代码 public class TransactionManage { private static ConcurrentDictionary <string, LocalTransaction> TransactionDic = new ConcurrentDictionary<string, LocalTransaction>(); private static ConcurrentDictionary<string, List<string>> TransitionIDMapDic = new ConcurrentDictionary

    46920编辑于 2023-07-19
领券