P
Paul Wu
From what I understand, in ASP.NET, each HTTP requests is serviced by a separate thread. So if my code uses a static Class with shared members and properties, I can manage concurrent access by using something like the Monitor or ReaderWriterLock class. It is rather difficult to simulate multiple threads in a debug environment so I am hoping someone can enlighten me by telling me what happens in the following scenario?
A request is made to the IIS which in turn launches a thread to access a shared member MyCollection.Count. Since the class has not been initialized, the Constructor is invoked and initializes class with some data from the database. Before the first thread exits the constructor, a second request is made to the IIS which spawns a second thread to call MyCollection.Count.
Will the second thread: (1) tries to invoke the constructor again, (2) waits till the first thread is finished before it enters MyCollection.Count, (3) gets access to MyCollection.Count and get a zero (or a number less than the actual count) because the collection is not fully initialized yet.
Public Class MyCollection
Private Shared mCollection as New Hashtable
Public Shared Function Count() as Integer
Return mCollection.Count
End Function
Shared Sub New
'code to initialize a datatable
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
End Sub
End Class
Should I be doing something like the following:
Public Classs MyCollection
Private Shared mCollection as New Hashtable
Private Shared Lock As New System.Threading.ReaderWriterLock
Public Shared Function Count() as Integer
Lock.AcquireReaderLock(Timeout.Infinite)
Count = mCollection.Count
Lock.ReleaseReaderLock()
End Function
Shared Sub New
Try
Lock.AcquireWriterLock(Timeout.Infinite)
'code to initialize a datatable
...
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
Finally
Lock.ReleaseWriterLock()
End Try
End Sub
End Class
A request is made to the IIS which in turn launches a thread to access a shared member MyCollection.Count. Since the class has not been initialized, the Constructor is invoked and initializes class with some data from the database. Before the first thread exits the constructor, a second request is made to the IIS which spawns a second thread to call MyCollection.Count.
Will the second thread: (1) tries to invoke the constructor again, (2) waits till the first thread is finished before it enters MyCollection.Count, (3) gets access to MyCollection.Count and get a zero (or a number less than the actual count) because the collection is not fully initialized yet.
Public Class MyCollection
Private Shared mCollection as New Hashtable
Public Shared Function Count() as Integer
Return mCollection.Count
End Function
Shared Sub New
'code to initialize a datatable
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
End Sub
End Class
Should I be doing something like the following:
Public Classs MyCollection
Private Shared mCollection as New Hashtable
Private Shared Lock As New System.Threading.ReaderWriterLock
Public Shared Function Count() as Integer
Lock.AcquireReaderLock(Timeout.Infinite)
Count = mCollection.Count
Lock.ReleaseReaderLock()
End Function
Shared Sub New
Try
Lock.AcquireWriterLock(Timeout.Infinite)
'code to initialize a datatable
...
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
Finally
Lock.ReleaseWriterLock()
End Try
End Sub
End Class