我有一个类,它包含在多线程应用程序中使用的列表(T)。我有三个方法Get、Add和Remove,它们访问并修改列表(of T)。我使用SyncLock来锁定m_List,无论是在查询所需对象时,还是在添加或删除对象时。
不过,我很好奇在添加对象或移除对象时,通过简单地锁定m_List是否有性能提升,而不是在搜索所需对象时?
Public Shared sub Add(SomeObject as object)
SyncLock ctype(m_List, IList).SyncRoot
m_List.add(SomeObject)
end SyncLock
end sub
Public Shared sub Remove(SearchString as string)
SyncLock ctype(m_List, IList).SyncRoot
m_List.RemoveAll(function(o as SomeObject) o.SomeProperty = SearchString)
end SyncLock
end Function
Public Shared Function Get(SearchString as string) as SomeObject
'The Commented out code is what I am thinking of removing...
'SyncLock ctype(m_List, IList).SyncRoot
Dim FoundObjectList = m_List.where(function(o as SomeObject) o.SomeProperty = SearchString)
if FoundObjectList.count > 0 then
If FoundObjectList(0).CreateDate < Now.AddMinutes(5) then
Remove(FoundObjectList(0).SomeProperty)
Return nothing
end if
else
Return FoundObjectList(0)
End if
Return Nothing
'end SyncLock
end sub