一提起lock,想必大家都很熟悉,因为它易用,顾名思义,就是一把锁,常用于多线程的同步,一次只允许一个线程进入。最近遇到一个很诡异的bug。
private static readonly object lock4 = new object(); private static void LoadResolvers(string name)
{
if (resolvesCache.Count == )
{ #if DEBUG
Console.WriteLine(name+",进入第一层判断,当前解析器数:" + resolvesCache.Count + ",时间:" + DateTime.Now.ToShortTimeString());
#endif lock (lock4)
{ if (resolvesCache.Count == )
{ #if DEBUG
Console.WriteLine(name + ",进入第二层判断,当前解析器数:" + resolvesCache.Count + ",时间:" + DateTime.Now.ToShortTimeString());
#endif List<Resolvers> listResolvers = ResolversBLL.GetAllResolvers(); #if DEBUG
Console.WriteLine(name + ",解析器查询完,准备遍历,查询出的解析器数:" + listResolvers.Count+ ",当前解析器数:" + resolvesCache.Count + ",时间:" + DateTime.Now.ToShortTimeString());
#endif foreach (Resolvers resolver in listResolvers)
{
List<ResolverConfigures> listConfigures = ResolverConfiguresBLL.GetResolverConfiguresByResolverId(resolver.ResolverID); LoginInfo loginInfo = LoginInfoDAL.SelectItemByItemId(resolver.ResolverID); NameValueCollection valueCollection = new NameValueCollection(); if (loginInfo != null)
{
valueCollection.Add("username", loginInfo.UserName);
valueCollection.Add("password", loginInfo.Password);
} foreach (ResolverConfigures configures in listConfigures)
{
if (!string.IsNullOrEmpty(configures.Key))
valueCollection.Add(configures.Key, configures.Value);
} if (!resolvesCache.ContainsKey(resolver))
{
resolvesCache.Add(resolver, valueCollection);
}
} #if DEBUG
Console.WriteLine(name + ",遍历完解析器并添加完成,当前解析器数:" + resolvesCache.Count + ",时间:" + DateTime.Now.ToShortTimeString());
#endif }
}
}
}
这段代码的大意:从数据库中查询出解析器(23行)加入到解析器缓存中(52行)。这个牵扯到多线程,因此,第12行加了把锁。本来数据库中只有13条数据,但是软件启动后,缓存中添加了26条数据,这是为什么呢?明明double if判断,lock每次只允许一个线程进入。
图1
为了搞清楚事情的真相,我写了个控制台代码:
private static Dictionary<string, string> resolvesCache = new Dictionary<string, string>();
public static Dictionary<string, string> ResolvesCache
{
get
{
if (resolvesCache.Count == )
{
LoadResolvers();
} return resolvesCache;
}
set
{
resolvesCache = value;
}
} private static void LoadResolvers()
{
if (resolvesCache.Count == )
{
lock (resolvesCache)
{
Thread.Sleep(new Random().Next(, )); for (int i = ; i < ; i++)
{
string key = i + DateTime.Now.Millisecond.ToString(); if (!resolvesCache.ContainsKey(key))
{
resolvesCache.Add(key, "wbq");
}
}
}
}
} static void Main(string[] args)
{ Thread thread1 = new Thread(new ThreadStart(() =>
{
Console.WriteLine("线程1:" + ResolvesCache.Count.ToString()); })); thread1.Start(); Thread thread2 = new Thread(new ThreadStart(() =>
{ LoadResolvers();
Console.WriteLine("线程2:" + ResolvesCache.Count.ToString()); })); thread2.Start(); Thread thread3 = new Thread(new ThreadStart(() =>
{
LoadResolvers();
Console.WriteLine("线程3:" + ResolvesCache.Count.ToString()); })); thread3.Start();
运行结果:
每次一个线程访问下缓存,缓存数据加倍变化,这是为什么呢?哦,别忘了double if判断。因为程序刚运行,三个线程几乎同时到达22行,过了第一个if。这好比,很多人在公司外面等着面试,大家赶时间点几乎同时到,但是面试是一对一进行,这时候需要等待。在24行之后,再加一个if判断:
运行结果:
这下跟正式代码一样了吧。都是double if 判断,测试代码达到要求了,为什么正式代码有问题呢?为了研究,在正式的代码上加上了好多debug,让它输出当前线程名称,记录相关日志。
从图1的日志上可以看出,这是同一个线程所为。为什么会执行两遍呢?
再看看23行代码:
List<Resolvers> listResolvers = ResolversBLL.GetAllResolvers(); 跟进到 ResolversBLL类中,发现了一句代码: private static OfficialMetadataResolveManager resolverManager = new OfficialMetadataResolveManager(BibliographyAutoUpdateProcess.ResolvesCache); 静态对象,类加载的时候,首先访问。 这不是解析器缓存的访问器吗?看看它的实现:
private static Dictionary<Resolvers, NameValueCollection> resolvesCache = new Dictionary<Resolvers, NameValueCollection>(); public static Dictionary<Resolvers, NameValueCollection> ResolvesCache
{
get
{
if (resolvesCache.Count == )
{
LoadResolvers(Thread.CurrentThread.Name);
} return resolvesCache;
}
set
{
resolvesCache = value;
}
}
第9行调用了 LoadResolvers,当前线程正在执行LoadResolvers方法,中途调用解析器缓存访问器,结果解析器缓存访问器又调用了此访问。所以这段代码执行了两次,因此,数据翻倍。终于真相大白了。
要修改其实也很简单,把第二个if判断,放到数据库查询解析器之后即可。这样的话,等于数据库查询了两次,但是缓存中只缓存一份数据。