C#根据需要加锁快速代码
//这句代码放到方法外面,全局一个就好
private static ConcurrentDictionary<string, object> dictionary = new ConcurrentDictionary<string, object>();
//根据已有条件设置key,因为要锁唯一的对象
var key = timeStr + siteId;
if (!dictionary.ContainsKey(key))
{
dictionary.TryAdd(key,new object());
}
lock (dictionary[key])
{
//锁住的代码
}
本文来自博客园,作者:静坐仰望星空,转载请注明原文链接:https://www.cnblogs.com/itljf/p/18200746