对象锁感悟
2012-07-18 14:09 田志良 阅读(2515) 评论(1) 编辑 收藏 举报1、对于一个对象,读取对象元素无须加锁,增加、修改、删除、遍历须加锁。
2、如果对象a包含另外一个对象b,针对对象a的操作只要锁住对象a,针对对象b的操作只要锁住对象b。
3、如果对象a包含对象b,对象b包含对象c,对c的操作可以锁住c,可以锁住b,也可以锁住a,至于锁哪个,据具体的业务逻辑性能要求来定。
4、有时为了防止并发覆盖,可扩大锁范围。
注意,锁住的对象不能为null。
用户缓存对象锁示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | internal class InternalUserCache : IUserCache { /// <summary> /// 添加用户 /// </summary> /// <param name="userId"></param> /// <param name="groupList"></param> /// <param name="friendList"></param> public void AddUser( string userId, List< string > groupList, List< string > friendList) { try { UserToken userToken = new UserToken(); userToken.GroupList = groupList; userToken.FriendsList = friendList; lock (BusinessCacheParameter.UserHash.SyncRoot) { BusinessCacheParameter.UserHash[userId] = userToken; } } catch (Exception ex) { throw new Exception( "InternalUserCache AddUser出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 添加用户好友 /// </summary> /// <param name="userId"></param> /// <param name="friendId"></param> public void AddUserFriend( string userId, string friendId) { try { UserToken userToken = (UserToken)BusinessCacheParameter.UserHash[userId]; if (userToken != null ) { lock (userToken) { List< string > friendsList = userToken.FriendsList; if (friendsList != null && !friendsList.Contains(friendId)) { friendsList.Add(friendId); } } } } catch (Exception ex) { throw new Exception( "InternalUserCache AddUserFriend出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 添加用户群组 /// </summary> /// <param name="userId"></param> /// <param name="groupId"></param> public void AddUserGroup( string userId, string groupId) { try { UserToken userToken = (UserToken)BusinessCacheParameter.UserHash[userId]; if (userToken != null ) { lock (userToken) { List< string > groupList = userToken.GroupList; if (groupList != null && !groupList.Contains(groupId)) { groupList.Add(groupId); } } } } catch (Exception ex) { throw new Exception( "InternalUserCache AddUserGroup出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 判断当前用户是否在线 /// true : 在线 /// false : 不在线 /// </summary> /// <param name="userId"></param> /// <returns></returns> public bool CheckOnLine( string userId) { try { if (BusinessCacheParameter.UserHash.Contains(userId)) { return true ; } else { return false ; } } catch (Exception ex) { throw new Exception( "InternalUserCache CheckOnLine出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 删除用户 /// </summary> /// <param name="userId"></param> public void DelUser( string userId) { try { lock (BusinessCacheParameter.UserHash.SyncRoot) { BusinessCacheParameter.UserHash.Remove(userId); } } catch (Exception ex) { throw new Exception( "InternalUserCache DelUser出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 删除用户好友 /// </summary> /// <param name="userId"></param> /// <param name="friendId"></param> public void DelUserFriend( string userId, string friendId) { try { UserToken userToken = (UserToken)BusinessCacheParameter.UserHash[userId]; if (userToken != null ) { lock (userToken) { List< string > friendsList = userToken.FriendsList; if (friendsList != null ) { friendsList.Remove(friendId); } } } } catch (Exception ex) { throw new Exception( "InternalUserCache DelUserFriend出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 删除用户群组 /// </summary> /// <param name="userId"></param> /// <param name="groupId"></param> public void DelUserGroup( string userId, string groupId) { try { UserToken userToken = (UserToken)BusinessCacheParameter.UserHash[userId]; if (userToken != null ) { lock (userToken) { List< string > groupList = userToken.GroupList; if (groupList != null ) { groupList.Remove(groupId); } } } } catch (Exception ex) { throw new Exception( "InternalUserCache DelUserGroup出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 获取所有在线人员 /// </summary> /// <returns></returns> public string [] GetOnlineUsers() { try { string [] userArray; lock (BusinessCacheParameter.UserHash.SyncRoot) { userArray = new string [BusinessCacheParameter.UserHash.Count]; BusinessCacheParameter.UserHash.Keys.CopyTo(userArray, 0); } return userArray; } catch (Exception ex) { throw new Exception( "InternalUserCache GetOnlineUsers出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 获取所有在线人数 /// </summary> /// <returns></returns> public int GetCountOfOnlineUsers() { try { return BusinessCacheParameter.UserHash.Count; } catch (Exception ex) { throw new Exception( "InternalUserCache GetCountOfOnlineUsers出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 获取用户所有好友 /// </summary> /// <param name="userId"></param> /// <returns></returns> public List< string > GetUserFriends( string userId) { try { List< string > returnResult = new List< string >(); UserToken userToken = (UserToken)BusinessCacheParameter.UserHash[userId]; if (userToken != null ) { lock (userToken) { if (userToken.FriendsList != null ) { foreach ( string str in userToken.FriendsList) { returnResult.Add(str); } } } } return returnResult; } catch (Exception ex) { throw new Exception( "InternalUserCache GetUserFriends出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 获取用户所有群组 /// </summary> /// <param name="userId"></param> /// <returns></returns> public List< string > GetUserGroups( string userId) { try { List< string > returnResult = new List< string >(); UserToken userToken = (UserToken)BusinessCacheParameter.UserHash[userId]; if (userToken != null ) { lock (userToken) { if (userToken.GroupList != null ) { foreach ( string str in userToken.GroupList) { returnResult.Add(str); } } } } return returnResult; } catch (Exception ex) { throw new Exception( "InternalUserCache GetUserGroups出错,错误描述为:" + ex.Message.ToString()); } } } |
[Serializable] internal class UserToken { #region 字段 private List< string > m_GroupList; //群组列表 private List< string > m_FriendList; //好友列表 #endregion #region 构造函数 /// <summary> /// 构造函数 /// </summary> public UserToken() { } #endregion #region 属性 /// <summary> /// 群组列表 /// </summary> internal List< string > GroupList { get { return this .m_GroupList; } set { this .m_GroupList = value; } } /// <summary> /// 好友列表 /// </summary> internal List< string > FriendsList { get { return this .m_FriendList; } set { this .m_FriendList = value; } } #endregion } |
群组缓存对象锁示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | internal class InternalGroupCache : IGroupCache { /// <summary> /// 获取群组所有成员 /// </summary> /// <param name="groupId"></param> /// <returns></returns> public List< string > GetGroupMembers( string groupId) { try { List< string > returnResult = new List< string >(); GroupToken groupToken = (GroupToken)BusinessCacheParameter.GroupHash[groupId]; if (groupToken != null ) { lock (groupToken) { if (groupToken.UserList != null ) { foreach ( string str in groupToken.UserList) { returnResult.Add(str); } } } } return returnResult; } catch (Exception ex) { throw new Exception( "InternalGroupCache GetGroupMembers出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 获取群组成员人数 /// </summary> /// <param name="groupId"></param> /// <returns></returns> public int GetCountOfGroupUsers( string groupId) { try { int returnResult = 0; GroupToken groupToken = (GroupToken)BusinessCacheParameter.GroupHash[groupId]; if (groupToken != null && groupToken.UserList != null ) { returnResult = groupToken.UserList.Count; } return returnResult; } catch (Exception ex) { throw new Exception( "InternalGroupCache GetCountOfGroupUsers出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 删除群组成员 /// </summary> /// <param name="groupId"></param> /// <param name="userId"></param> public void DelGroupMember( string groupId, string userId) { try { GroupToken groupToken = (GroupToken)BusinessCacheParameter.GroupHash[groupId]; if (groupToken != null ) { lock (groupToken) { List< string > userList = groupToken.UserList; if (userList != null ) { userList.Remove(userId); } } } } catch (Exception ex) { throw new Exception( "InternalGroupCache DelGroupMember出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 删除群组 /// </summary> /// <param name="groupId"></param> public void DelGroup( string groupId) { try { lock (BusinessCacheParameter.GroupHash.SyncRoot) { BusinessCacheParameter.GroupHash.Remove(groupId); } } catch (Exception ex) { throw new Exception( "InternalGroupCache DelGroup出错,错误描述为:" + ex.Message.ToString()); } } /// <summary> /// 添加群组成员 /// </summary> /// <param name="groupId"></param> /// <param name="userId"></param> public void AddGroupMember( string groupId, string userId) { try { // 此处扩大锁范围,防止相同群组下的不同用户出现并发覆盖 lock (BusinessCacheParameter.GroupHash.SyncRoot) { GroupToken groupToken = (GroupToken)BusinessCacheParameter.GroupHash[groupId]; if (groupToken != null ) { List< string > userList = groupToken.UserList; if (userList == null ) { userList = new List< string >(); userList.Add(userId); groupToken.UserList = userList; } else { if (!userList.Contains(userId)) { userList.Add(userId); } } } else { groupToken = new GroupToken(); List< string > userList = new List< string >(); userList.Add(userId); groupToken.UserList = userList; BusinessCacheParameter.GroupHash[groupId] = groupToken; } } } catch (Exception ex) { throw new Exception( "InternalGroupCache AddGroupMember出错,错误描述为:" + ex.Message.ToString()); } } } |
[Serializable] internal class GroupToken { #region 字段 private List< string > m_UserList; //用户列表 #endregion #region 构造函数 /// <summary> /// 构造函数 /// </summary> public GroupToken() { } #endregion #region 属性 /// <summary> /// 用户列表 /// </summary> internal List< string > UserList { get { return this .m_UserList; } set { this .m_UserList = value; } } #endregion } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?