473,386 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

clarification on Shared class constructor

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

Nov 19 '05 #1
2 1183
Paul:
Static constructors are thread-safe, no if or buts about it. That means,
from a static constructor point of view, you are assured that...the only
thing you need to worry about is if your static constructor relies on a 2nd
static constructor which has a reference back to the 1st class (cyclical
reference)...otherwise your safe.

As for as your properties, they aren't thread-safe...unless the underlying
class itself is (some .net classes are thread safe).

You should take a look at:
http://odetocode.com/Articles/313.aspx
and
http://odetocode.com/Articles/314.aspx

As you hopefully know, locks come with a hefty performance price and you
should consider alternatives if possible...I haven't had the need to use
static fields too often...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Paul Wu" <wu**********@gmail.com> wrote in message
news:uU*************@TK2MSFTNGP12.phx.gbl...
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
Nov 19 '05 #2
Thanks Karl:

The article really helped.

Paul Wu

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:Op**************@TK2MSFTNGP14.phx.gbl...
Paul:
Static constructors are thread-safe, no if or buts about it. That means,
from a static constructor point of view, you are assured that...the only
thing you need to worry about is if your static constructor relies on a
2nd static constructor which has a reference back to the 1st class
(cyclical reference)...otherwise your safe.

As for as your properties, they aren't thread-safe...unless the underlying
class itself is (some .net classes are thread safe).

You should take a look at:
http://odetocode.com/Articles/313.aspx
and
http://odetocode.com/Articles/314.aspx

As you hopefully know, locks come with a hefty performance price and you
should consider alternatives if possible...I haven't had the need to use
static fields too often...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Paul Wu" <wu**********@gmail.com> wrote in message
news:uU*************@TK2MSFTNGP12.phx.gbl...
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

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
2
by: Paul Wu | last post by:
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...
10
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global'...
15
by: Rob Nicholson | last post by:
A consequence of the ASP.NET architecture on IIS has just hit home with a big thud. It's to do with shared variables. Consider a module like this: Public Module Functions Public GlobalName As...
5
by: Erik Cruz | last post by:
Hello! I have read some threads discussing the fact that a module is in reality a shared class. If I try to create a Public Shared Class in vb.net I receive a compile error. Why? If I can't...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.