在类B的方法中使用lock操作,lock里面操作的是另一个类里面的静态Dictionary
想问一下,这样写,这个lock到底有没有起作用呢?会不会出现什么问题?
public class Globle
{
public class Globle
{
public static Dictionary<string, AppSession> sessionDict = new Dictionary<string, AppSession>();
public static Dictionary<int, List<string>> companyDataDict = new Dictionary<int, List<string>>();
}
}
public class B
{
public void RegisterNewSession(AppSession session)
{
if (Globle.sessionDict.ContainsKey(session.user.UserID))
return;
lock (obj_1)
{
Globle.sessionDict.Add(session.user.UserID, session);
if (Globle.companyDataDict.Keys.Contains(session.user.CompanyID))
{
Globle.companyDataDict[session.user.CompanyID].Add(session.user.UserID);
}
}
}
}