麦田 网站建设,如何建立自己的网络销售,域名买好了怎么做网站,福州建网站好久不见#xff0c;马甲哥封闭居家半个月#xff0c;记录之前遇到的一件小事。ConcurrentDictionaryTKey,TValue绝大部分api都是线程安全的[1]#xff0c;唯二的例外是接收工厂函数的api#xff1a;AddOrUpdate、GetOrAdd#xff0c;这两个api不是线程安全的马甲哥封闭居家半个月记录之前遇到的一件小事。ConcurrentDictionaryTKey,TValue绝大部分api都是线程安全的[1]唯二的例外是接收工厂函数的apiAddOrUpdate、GetOrAdd这两个api不是线程安全的需要引起重视。All these operations are atomic and are thread-safe with regards to all other operations on the ConcurrentDictionaryTKey,TValue class. The only exceptions are the methods that accept a delegate, that is, AddOrUpdate and GetOrAdd.之前有个同事就因为这个case背了一个P。AddOrUpdate(TKey, TValue, FuncTKey,TValue,TValue valueFactory);GetOrAdd(TKey key, FuncTKey, TValue valueFactory);注意包括其他接收工厂委托的重载函数整个过程中涉及与字典直接交互的都用到到精细锁valueFactory工厂函数在锁定区外面被执行因此这些代码不受原子性约束。Q1: valueFactory工厂函数不在锁定范围为什么不在锁范围A: 还不是因为微软不相信你能写出健壮的业务代码未知的业务代码可能造成死锁。However, delegates for these methods are called outside the locks to avoid the problems that can arise from executing unknown code under a lock. Therefore, the code executed by these delegates is not subject to the atomicity of the operation.Q2带来的效果• valueFactory工厂函数可能会多次执行• 虽然会多次执行 但插入的值固定是一个插入的值取决于哪个线程率先插入字典。Q3: 怎么做到的随机稳定输出一列值A源代码做了double check[2]了后续线程通过工厂类创建值后会再次检查字典发现已有值会丢弃自己创建的值。示例代码using System.Collections.Concurrent;public class Program
{private static int _runCount 0;private static readonly ConcurrentDictionarystring, string _dictionary new ConcurrentDictionarystring, string();public static void Main(string[] args){var task1 Task.Run(() PrintValue(The first value));var task2 Task.Run(() PrintValue(The second value));var task3 Task.Run(() PrintValue(The three value));var task4 Task.Run(() PrintValue(The four value));Task.WaitAll(task1, task2, task4,task4);PrintValue(The five value);Console.WriteLine($Run count: {_runCount});}public static void PrintValue(string valueToPrint){var valueFound _dictionary.GetOrAdd(key,x {Interlocked.Increment(ref _runCount);Thread.Sleep(100);return valueToPrint;});Console.WriteLine(valueFound);}
}上面4个线程并发插入字典每次随机输出_runCount4显示工厂类执行4次。Q4如果工厂产值的代价很大不允许多次创建如何实现笔者的同事之前就遇到这样的问题高并发请求频繁创建redis连接直接打挂了机器。A: 有一个trick能解决这个问题: valueFactory工厂函数返回Lazy容器.using System.Collections.Concurrent;public class Program
{private static int _runCount2 0;private static readonly ConcurrentDictionarystring, Lazystring _lazyDictionary new ConcurrentDictionarystring, Lazystring();public static void Main(string[] args){task1 Task.Run(() PrintValueLazy(The first value));task2 Task.Run(() PrintValueLazy(The second value));task3 Task.Run(() PrintValueLazy(The three value));task4 Task.Run(() PrintValueLazy(The four value)); Task.WaitAll(task1, task2, task4, task4);PrintValue(The five value);Console.WriteLine($Run count: {_runCount2});}public static void PrintValueLazy(string valueToPrint){var valueFound _lazyDictionary.GetOrAdd(key,x new Lazystring(() {Interlocked.Increment(ref _runCount2);Thread.Sleep(100);return valueToPrint;}));Console.WriteLine(valueFound.Value);}
}上面示例依旧会随机稳定输出但是_runOut1表明产值动作只执行了一次、valueFactory工厂函数返回Lazy容器是一个精妙的trick。① 工厂函数依旧没进入锁定过程会多次执行;② 与最上面的例子类似只会插入一个Lazy容器(后续线程依旧做double check发现字典key已经有Lazy容器了,会放弃插入)③ 线程执行Lazy.Value, 这时才会执行创建value的工厂函数④ 多个线程尝试执行Lazy.Value, 但这个延迟初始化方式被默认设置为ExecutionAndPublication不仅以线程安全的方式执行, 而且确保只会执行一次构造函数。public Lazy(FuncT valueFactory):this(valueFactory, LazyThreadSafetyMode.ExecutionAndPublication, useDefaultConstructor: false)
{
}控制构造函数执行的枚举值描述ExecutionAndPublication[3]能确保只有一个线程能够以线程安全方式执行构造函数None线程不安全Publication并发线程都会执行初始化函数以先完成初始化的值为准IHttpClientFactory在构建命名HttpClient,活跃连接Handler字典时 也用到了这个技巧大家自行欣赏DefaultHttpCLientFactory源码[4]。• https://andrewlock.net/making-getoradd-on-concurrentdictionary-thread-safe-using-lazy/总结为解决ConcurrentDictionary GetOrAdd(key, valueFactory) 工厂函数在并发场景下被多次执行的问题:① valueFactory工厂函数产生Lazy容器② 将Lazy容器的值初始化姿势设定为ExecutionAndPublication(线程安全且执行一次)。两姿势缺一不可。本人会不时修正理解、更正错误请适时移步左下角永久更新地址也请看客大胆斧正。引用链接[1] ConcurrentDictionaryTKey,TValue绝大部分api都是线程安全的: https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2?viewnet-6.0[2] double check: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs#L1152[3] ExecutionAndPublication: https://docs.microsoft.com/en-us/dotnet/api/system.threading.lazythreadsafetymode?viewnet-6.0#system-threading-lazythreadsafetymode-executionandpublication[4] DefaultHttpCLientFactory源码: https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Http/src/DefaultHttpClientFactory.cs#L118