eas*_*der 9 c# multithreading caching dictionary locking
我对正在阅读的书中的代码列表,Nutshell中的C#3以及线程感到困惑.在关于应用程序服务器中的线程安全的主题中,下面的代码是作为UserCache的示例给出的:
static class UserCache
{
static Dictionary< int,User> _users = new Dictionary< int, User>();
internal static User GetUser(int id)
{
User u = null;
lock (_users) // Why lock this???
if (_users.TryGetValue(id, out u))
return u;
u = RetrieveUser(id); //Method to retrieve from databse
lock (_users) _users[id] = u; //Why lock this???
return u;
}
}
Run Code Online (Sandbox Code Playgroud)
作者解释了为什么RetrieveUser方法没有锁定,这是为了避免长时间锁定缓存.
我很困惑为什么要锁定TryGetValue和字典的更新,因为即使上面的字典正在被更新两次,如果2个线程同时使用相同的未检索的id调用.
通过锁定字典读取实现了什么?
非常感谢您提出的所有意见和见解.
| 归档时间: |
|
| 查看次数: |
6039 次 |
| 最近记录: |