Is a private collection in an instance of a class, threadsafe?
In order to illustrate my question, please have a look at the following "stupid" code.
As you will see "myClass" instantiates a "private" collection.
"anotherClass" instantiates "myClass" and populates the collection, then passes the instance of "myClass" to a shared method of another class "aThirdClass", which in turn "gets" and "sets" the contents of the collection.
So, the question, is this code ThreadSafe or do I systematically have to use the SyncLock, each time I need to get access to the contents of the hashTable?
Public Class myClass
Private _myCollection As HashTable = New HashTable(StringComparer.OrdinalIgnoreCase)
Public Sub AddValue(ByVal key As String, ByVal value As String)
If (Not _myCollection.ContainsKey(key)) Then
_myCollection.Add(key, value)
End If
End Sub
Public Function GetValue(ByVal key As String) As String
Dim strValue As String = String.Empty
If (_myCollection.ContainsKey(key)) Then
strValue = _myCollection(key)
End If
Return strValue
End Function
End Class
Public Class anotherClass
Public Shared Sub myRoutine()
Dim anInstance As myClass = New myClass()
anInstance.AddValue("key1", "value1")
anInstance.AddValue("key2", "value2")
aThirdClass.DoSomething(anInstance)
If (Not String.IsNullOrEmpty(anInstance.getValue("key3")) Then
End If
End Sub
End Class
Public Class aThirdClass
Public Shared Sub DoSomething(ByVal theInstance As myClass)
Dim strValue As String = theInstance.GetValue("key2")
theInstance.AddValue("key3", "value3")
End Sub
End Class
boeledi
Member
38 Points
26 Posts
Is a private collection in a class, thread safe?
Feb 09, 2013 02:12 PM|LINK
Hi,
Is a private collection in an instance of a class, threadsafe?
In order to illustrate my question, please have a look at the following "stupid" code.
As you will see "myClass" instantiates a "private" collection.
"anotherClass" instantiates "myClass" and populates the collection, then passes the instance of "myClass" to a shared method of another class "aThirdClass", which in turn "gets" and "sets" the contents of the collection.
So, the question, is this code ThreadSafe or do I systematically have to use the SyncLock, each time I need to get access to the contents of the hashTable?
anInstance.AddValue("key1", "value1") anInstance.AddValue("key2", "value2") aThirdClass.DoSomething(anInstance) If (Not String.IsNullOrEmpty(anInstance.getValue("key3")) ThenEnd If End Sub End Class Public Class aThirdClass Public Shared Sub DoSomething(ByVal theInstance As myClass) Dim strValue As String = theInstance.GetValue("key2") theInstance.AddValue("key3", "value3") End Sub End ClassMany thanks for your feedback
dahla
Participant
1816 Points
369 Posts
Re: Is a private collection in a class, thread safe?
Feb 10, 2013 02:37 AM|LINK
Try reading this article http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx
Using a hashtable that is a threadsafe as you can get, like described in the article, without adding some kind of synchronization